python - 在大 txt 文件 pandas 中查找标题
问题描述
假设我有一个包含几百万行的大文件。前 300+ 行(可变数字)包含有关文件的信息,然后在数据之前有一个标题行。我不知道标题在哪一行,但我知道它的开头。这是我的数据示例:
#This File contains some cool suff
#We will see what line the header is on
#Maybe it is in this line
#CHROM POS ID
1 100 17
2 200 18
2 300 18
标题行是#CHROM POS ID
这是我尝试过的,但它返回list index out of range
:
database = pd.read_table(infile, header=[num for num,line in enumerate(infile) if line.startswith("#CHROM")])
我想我天真地假设它的pd.read_table
运作方式with open()
与可能有效。任何帮助将不胜感激!
解决方案
编辑:刚刚看到它是一个文本文件
将变量设置为标题行,
lineno = 0
for line in infile.readlines():
if line.startswith('#CHROM'):
headerrow = lineno
lineno += 1
然后,当您引入文件时,您可以执行诸如 pd.read_table('my_file.txt', header = headerrow) 之类的操作以及您需要的任何其他参数。
推荐阅读
- linux - 在 Fedora (Linux) 中自动将屏幕截图复制到剪贴板
- amazon-web-services - 我可以将 cloudfront 与 route53 流量策略一起使用吗?
- c++ - 保存或放弃用户对对象的更改
- php - 如何从当前订单中获取 WooCommerce 订单 ID?
- php - 来自 db 的重音字符在服务器迁移后未立即显示
- python - 为什么附加到字典中的列表会产生不同的结果?
- vue.js - npm install package throws error The operation was denied by your operating system
- reactjs - 如何使用 Material UI Cards 将按钮连接到表单提交?
- c++ - Qt 和 Arduino 串行通信(读写)
- fortran - 编译后的英特尔编译器 XE 19 在 Windows 10 AMD Ryzen 上无法使用 /Qparallel 出现错误 0xc000007b