python pandas read_csv quotechar不工作 [英] python pandas read_csv quotechar does not work

查看:1301
本文介绍了python pandas read_csv quotechar不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读帖子,但尽管我不知道为什么 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 +'的分隔符将
解释为正则表达式,将强制使用python $ b $

$ c>默认设置为逗号):

  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 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!!

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆