看上去here和here这些接近我相信我看到的核心问题,但以其他方式得到修复。大熊猫read_csv()与sep和quotechar冲突导致意外的列数
我想解析一个CSV,它有一个现在需要有逗号的字段,需要我们用引号包住这个字段。这是引号中唯一的字段。
我们的分隔符(sep)是逗号,现在我们正在添加引号(quotechar)的字符串分隔符。
我把它煮成这个。在我看来,sep和quotechar应用程序的顺序是导致使用quotechar的行的关键问题,在它们中使用sep将无法工作。
带有最后一行注释的数据文件。
$ cat simple.csv
column1,column2, column3
one, two, three
one, two, "three"
#one, "two, two_again", three
$
代码:
df = pd.read_csv(simple_file, sep=',', header=0, comment='#', quotechar='"')
print df
输出:
column1 column2 column3
0 one two three
1 one two "three"
现在,添加它具有引用字符串中的字符九月的最后一行。
数据文件:
$ cat simple.csv
column1,column2, column3
one, two, three
one, two, "three"
one, "two, two_again", three
$
输出失败:
pandas/parser.pyx in pandas.parser.raise_parser_error (pandas/parser.c:22649)()
CParserError: Error tokenizing data. C error: Expected 3 fields in line 4, saw 4
我相信,我想迫使大熊猫首先使用在每一行的报价分隔符,然后使用分隔符,因为它是做相反。似乎无法弄清楚如何。有没有办法告诉熊猫这是我找不到的?
摆脱空间或定义的空间作为分隔符的一部分。然后,只需指定header = None就可以读取文件,其余的默认值将被处理。 – pvg