我正在解析一个txt文件,该文件使用“#@#@#”作为更改行,“~”用于列分离。我仍然可以使用pd.read_csv()来解析它以获得一个DataFrame吗?
发布于 2017-05-04 10:20:25
行终止器的主要问题是长度必须是1,所以在read_csv之后可能使用过滤。
temp=u"""a~h~h#@#@#a~h~h#@#@#"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp), lineterminator='#', sep='~', header=None)
print (df)
0 1 2
0 a h h
1 @ NaN NaN
2 @ NaN NaN
3 a h h
4 @ NaN NaN
5 @ NaN NaN
df = df[df.iloc[:,0] != '@']
print (df)
0 1 2
0 a h h
3 a h hhttps://stackoverflow.com/questions/43779982
复制相似问题