2014-10-27 52 views
13

我有一個輸入文件,其中每個值都以字符串形式存儲。 它位於csv文件中,每個條目在雙引號內。csv中的雙引號元素不能讀熊貓

示例文件:

"column1","column2", "column3", "column4", "column5", "column6" 
"AM", "07", "1", "SD", "SD", "CR" 
"AM", "08", "1,2,3", "PR,SD,SD", "PR,SD,SD", "PR,SD,SD" 
"AM", "01", "2", "SD", "SD", "SD" 

只有六列。我需要輸入什麼選項來讀熊貓read_csv才能正確讀取?

我目前正在嘗試:

import pandas as pd 
df = pd.read_csv(file, quotechar='"') 

但是這給我的錯誤信息: CParserError: Error tokenizing data. C error: Expected 6 fields in line 3, saw 14

這顯然意味着它忽視了「'和分析每一個逗號作爲字段 (「1,2,3」,「PR,SD,SD」,「PR,SD,SD」,「PR,SD,SD」)對於第3行,第3列至第6列應爲帶逗號的字符串。 )

如何獲得pandas.read_csv來正確解析?

謝謝。

+0

如果你善於用正則表達式,你可以在九月arguement用它來read_csv ... http://stackoverflow.com/questions/24091356/pandas-read-csv-with-final-column-containing -commas – rhaskett 2014-10-27 23:38:08

回答

7

這將工作。它回退到python解析器(因爲你有非常規的分隔符,例如它們是逗號,有時候是空格)。如果你只有逗號,它會使用C語法分析器,速度會更快。

In [1]: import csv 

In [2]: !cat test.csv 
"column1","column2", "column3", "column4", "column5", "column6" 
"AM", "07", "1", "SD", "SD", "CR" 
"AM", "08", "1,2,3", "PR,SD,SD", "PR,SD,SD", "PR,SD,SD" 
"AM", "01", "2", "SD", "SD", "SD" 

In [3]: pd.read_csv('test.csv',sep=',\s+',quoting=csv.QUOTE_ALL) 
pandas/io/parsers.py:637: ParserWarning: Falling back to the 'python' engine because the 'c' engine does not support regex separators; you can avoid this warning by specifying engine='python'. 
    ParserWarning) 
Out[3]: 
    "column1","column2" "column3" "column4" "column5" "column6" 
"AM"    "07"  "1"  "SD"  "SD"  "CR" 
"AM"    "08" "1,2,3" "PR,SD,SD" "PR,SD,SD" "PR,SD,SD" 
"AM"    "01"  "2"  "SD"  "SD"  "SD"