我正试图通过python中熊猫的read_csv读取一个文本文件我的文本文件看起来像(所有数值都是数字):
35 61 7 1 0 # with leading white spaces
0 1 1 1 1 1 # with leading white spaces
33 221 22 0 1 # without leading white spaces
233 2 # without leading white spaces
1(01-02),2(02-03),3(03-04) # this line cause 'Error tokenizing data. C error: Expected 1 fields in line 5, saw 3
我的python代码如下:
import pandas as pd
df = pd.read_csv('example.txt', header=None)
df
输出如下:
CParserError: 'Error tokenizing data. C error: Expected 1 fields in line 5, saw 3
在处理前导空格之前,我需要先处理“标记数据时出错”问题所以我改了代码如下:
import pandas as pd
df = pd.read_csv('example.txt', header=None, error_bad_lines=False)
df
我可以像我想的那样用前导空格来获取数据,但是第5行的数据已经消失了。输出如下:
b'Skipping line 5: expected 1 fields, saw 3\n
35 61 7 1 0 # with leading white spaces as intended
0 1 1 1 1 1 # with leading white spaces as intended
33 221 22 0 1 # without leading white spaces
233 2 # without leading white spaces
# 5th line disappeared (not my intention).
所以我想把下面的代码改成第五行。
import pandas as pd
df = pd.read_csv('example.txt', header=None, sep=':::', engine='python')
df
我成功地获得了第5行的数据,但第1行和第2行的前导空格如下所示:
35 61 7 1 0 # without leading white spaces(not my intention)
0 1 1 1 1 1 # without leading white spaces(not my intention)
33 221 22 0 1 # without leading white spaces
233 2 # without leading white spaces
1(01-02),2(02-03),3(03-04) # I successfully got this line as intended.
我看到了几篇关于用字符串保留前导空格的文章,但是我找不到用数字保留前导空格的案例谢谢你的帮助。
最佳答案
钥匙在分离器里。如果将sep
指定为regex^
开始行元字符,则此操作有效。
s = pd.read_csv('example.txt', header=None, sep='^', squeeze=True)
s
0 35 61 7 1 0
1 0 1 1 1 1 1
2 33 221 22 0 1
3 233 2
4 1(01-02),2(02-03),3(03-04)
Name: 0, dtype: object
s[1]
' 0 1 1 1 1 1'