熊猫提取注释行

jay*_*psb 4 python pandas

我有一个包含前几行注释和实际数据的数据文件。

#param1 : val1
#param2 : val2
#param3 : val3
12
2
1
33
12
0
12
...
Run Code Online (Sandbox Code Playgroud)

我可以将数据读取为 pandas.read_csv(filename, comment='#',header=None). 但是,我也希望单独阅读注释行以提取读取参数值。到目前为止,我只遇到跳过或删除注释行的情况,但是如何单独提取注释行?

Ell*_*iot 5

在打电话给read_csv你真的不能。如果您只是处理标题,您可以打开文件,提取注释行并处理它们,然后在单独的调用中读入数据。

from itertools import takewhile
with open(filename, 'r') as fobj:
    # takewhile returns an iterator over all the lines 
    # that start with the comment string
    headiter = takewhile(lambda s: s.startswith('#'), fobj)
    # you may want to process the headers differently, 
    # but here we just convert it to a list
    header = list(headiter)
df = pandas.read_csv(filename)
Run Code Online (Sandbox Code Playgroud)