python pandas read_csv quotechar不工作 [英] python pandas read_csv quotechar does not work
问题描述
我已阅读这,此和这帖子,但尽管我不知道为什么 quotechar
在 pd.read_csv()
(Python 3,pandas 0.18.0和0.18.1)中不起作用。如何读取这样的数据框架:
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
我想要以下结果:
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
谢谢!
Pandas doc read_csv()
:
长度大于1个字符且不同于'\s +'的分隔符将
$ c>默认设置为逗号):
解释为正则表达式,将强制使用python $ b $pd.read_csv(file,skipinitialspace = True,quotechar =' )
I've read this, this and this posts but despite I don't know why
quotechar
does not work atpd.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!!
解决方案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 = '"')
这篇关于python pandas read_csv quotechar不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!