熊猫因可变列而失败

Vin*_*jan 4 python file multiple-columns pandas

我的文件是这个

    4 7 a a
    s g 6 8 0 d
    g 6 2 1 f 7 9 
    f g 3 
    1 2 4 6 8 9 0
Run Code Online (Sandbox Code Playgroud)

我正在使用熊猫以熊猫对象的形式保存它。但我收到以下错误
pandas.parser.CParserError: Error tokenizing data. C error: Expected 6 fields in line 3, saw 8

我使用的代码是
file = pd.read_csv("a.txt",dtype = None,delimiter = " ")

任何人都可以提出一个包含文件的想法吗?

Zer*_*ero 5

这是一种方法。

In [50]: !type temp.csv
4,7,a,a
s,g,6,8,0,d
g,6,2,1,f,7,9
f,g,3
1,2,4,6,8,9,0
Run Code Online (Sandbox Code Playgroud)

读取 csv 到列表列表,然后转换为 DataFrame。

In [51]: pd.DataFrame([line.strip().split(',') for line in open('temp.csv', 'r')])
Out[51]:
   0  1  2     3     4     5     6
0  4  7  a     a  None  None  None
1  s  g  6     8     0     d  None
2  g  6  2     1     f     7     9
3  f  g  3  None  None  None  None
4  1  2  4     6     8     9     0
Run Code Online (Sandbox Code Playgroud)