假设我有一个包含几百万行的大文件.前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
这是我尝试过但它返回列表索引超出范围:
database = pd.read_table(infile, header=[num for num,line in enumerate(infile) if line.startswith("#CHROM")])
我想我天真地认为pd.read_table的运行方式与open()相同,并且可能有效.任何帮助将不胜感激!
最佳答案 编辑:刚看到它是一个文本文件
将变量设置为headerrow,
lineno = 0
for line in infile.readlines():
if line.startswith('#CHROM'):
headerrow = lineno
lineno += 1
然后当你引入文件时,你可以做一些像pd.read_table(‘my_file.txt’,header = headerrow)以及你需要的任何其他参数.