问题
I have a file looking like
# Comment 1
# Comment 2
# A B C
1 2 3
4 5 6
7 8 9
How to read it with python pandas module, so as the last line of comments can be interpreted as the columns titles ?
I've tried
pandas.read_table(file_path, header= 2 , comment='#' )
But the comment lines are eliminated first, thus the header line will be
7 8 9
回答1:
You can do this manually: first read the comments, parse the column names, then call read_table
:
import itertools
import pandas as pd
def read_data(path):
with open(path) as handle:
*_comments, names = itertools.takewhile(
lambda line: line.startswith('#'), handle)
# This is not the most robust way, adjust for your needs :)
names = names[1:].split()
return pandas.read_table(path, header=0, names=names, sep=' ', comment='#')
回答2:
In [7]: pd.read_csv('test.csv',skiprows=2,sep='\s+',escapechar='#')
Out[7]:
A B C
0 1 2 3
1 4 5 6
2 7 8 9
escapechar
tell that #
must be consider as a end of field. Here it is used as a clean workaround. sep='\s+'
is required here because you have trailing space after 3 and 6 in your file (or this page.)
来源:https://stackoverflow.com/questions/36772656/get-one-specific-line-of-comment-as-header-with-python-pandas