首页 > 解决方案 > 在大 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()与可能有效。任何帮助将不胜感激!

标签: pythonpandas

解决方案


编辑:刚刚看到它是一个文本文件

将变量设置为标题行,

lineno = 0
for line in infile.readlines():
    if line.startswith('#CHROM'):
        headerrow = lineno
    lineno += 1

然后,当您引入文件时,您可以执行诸如 pd.read_table('my_file.txt', header = headerrow) 之类的操作以及您需要的任何其他参数。


推荐阅读