Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 提取注释行_Python_Pandas - Fatal编程技术网

Python 提取注释行

Python 提取注释行,python,pandas,Python,Pandas,我有一个数据文件,其中包含前几行注释,然后是实际数据 #param1 : val1 #param2 : val2 #param3 : val3 12 2 1 33 12 0 12 ... 我可以将数据读取为pandas.read_csv(文件名,comment='#',header=None)。但是,我也希望单独读取注释行,以便提取和读取参数值。到目前为止,我只遇到跳过或删除注释行,但如何也单独提取注释行?在调用read\u csv时,你真的不能。如果您只是在处理一个标题,您可以打开文件,提取

我有一个数据文件,其中包含前几行注释,然后是实际数据

#param1 : val1
#param2 : val2
#param3 : val3
12
2
1
33
12
0
12
...

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

在调用
read\u 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)

也许你可以用正常的方式再次读取这个文件,读取每一行来获取你的参数

def get_param( filename):
    para_dic = {}
    with  open(filename,'r') as cmt_file:    # open file
        for line in cmt_file:    # read each line
            if line[0] == '#':    # check the first character
                line = line[1:]    # remove first '#'
                para = line.split(':')     # seperate string by ':'
                if len(para) == 2:
                    para_dic[ para[0].strip()] = para[1].strip()
    return para_dic
{'param3': 'val3', 'param2': 'val2', 'param1': 'val1'}
此函数将返回包含参数的字典

def get_param( filename):
    para_dic = {}
    with  open(filename,'r') as cmt_file:    # open file
        for line in cmt_file:    # read each line
            if line[0] == '#':    # check the first character
                line = line[1:]    # remove first '#'
                para = line.split(':')     # seperate string by ':'
                if len(para) == 2:
                    para_dic[ para[0].strip()] = para[1].strip()
    return para_dic
{'param3': 'val3', 'param2': 'val2', 'param1': 'val1'}