16

我无法创建在使用时已转义引号的数据框read_csv
(注意:Rread.csv按预期工作。)

我的代码:

import pandas as pd
pd.read_csv('data.csv')
#error!
CParserError: Error tokenizing data. C error: Expected 2 fields in line 4, saw 3

数据.csv

SEARCH_TERM,ACTUAL_URL
"bra tv bord","http://www.ikea.com/se/sv/catalog/categories/departments/living_room/10475/?se%7cps%7cnonbranded%7cvardagsrum%7cgoogle%7ctv_bord"
"tv på hjul","http://www.ikea.com/se/sv/catalog/categories/departments/living_room/10475/?se%7cps%7cnonbranded%7cvardagsrum%7cgoogle%7ctv_bord"
"SLAGBORD, \"Bergslagen\", IKEA:s 1700-tals serie","http://www.ikea.com/se/sv/catalog/categories/departments/living_room/10475/?se%7cps%7cnonbranded%7cvardagsrum%7cgoogle%7ctv_bord"

如何阅读此 csv 并避免此错误?

我的猜测是 pandas 正在使用一些正则表达式,这些表达式无法处理第三行的歧义和行程,或者更具体地说:\"Bergslagen\".

4

1 回答 1

26

它确实有效,但您必须为嵌入的引号指明转义字符:

In [1]: data = '''SEARCH_TERM,ACTUAL_URL
"bra tv bord","http://www.ikea.com/se/sv/catalog/categories/departments/living_room/10475/?se%7cps%7cnonbranded%7cvardagsrum%7cgoogle%7ctv_bord"
"tv p\xc3\xa5 hjul","http://www.ikea.com/se/sv/catalog/categories/departments/living_room/10475/?se%7cps%7cnonbranded%7cvardagsrum%7cgoogle%7ctv_bord"
"SLAGBORD, \\"Bergslagen\\", IKEA:s 1700-tals serie","http://www.ikea.com/se/sv/catalog/categories/departments/living_room/10475/?se%7cps%7cnonbranded%7cvardagsrum%7cgoogle%7ctv_bord"'''

In [2]: df = read_csv(StringIO(data), escapechar='\\', encoding='utf-8')

In [3]: df
Out[3]: 
                                      SEARCH_TERM                                         ACTUAL_URL
0                                     bra tv bord  http://www.ikea.com/se/sv/catalog/categories/d...
1                                      tv på hjul  http://www.ikea.com/se/sv/catalog/categories/d...
2  SLAGBORD, "Bergslagen", IKEA:s 1700-tals serie  http://www.ikea.com/se/sv/catalog/categories/d...

看到这个要点

于 2012-12-11T17:21:01.427 回答