python pandas read_csv quotechar does not work

匿名 (未验证) 提交于 2019-12-03 01:25:01

问题:

I've read this, this and this posts but despite I don't know why quotechar does not work at pd.read_csv() (Python 3, pandas 0.18.0 and 0.18.1). And how could I read a dataframe like this:

"column1","column2", "column3", "column4", "column5", "column6" "AM", 7, "1", "SD", "SD", "CR" "AM", 8, "1,2 ,3", "PR, SD,SD", "PR ; , SD,SD", "PR , ,, SD ,SD" "AM", 1, "2", "SD", "SD", "SD" 

I want the following result:

Out[116]:    column1  column2 column3    column4       column5        column6 0      AM        7       1         SD            SD             CR 1      AM        8  1,2 ,3  PR, SD,SD  PR ; , SD,SD  PR , ,, SD,SD 2      AM        1       2         SD            SD             SD 

Thank you!!

回答1:

Pandas doc on separators in read_csv():

Separators longer than 1 character and different from '\s+' will be interpreted as regular expressions, will force use of the python parsing engine and will ignore quotes in the data.

Try using this instead (sep by default set to a comma):

pd.read_csv(file, skipinitialspace = True, quotechar = '"') 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!