我读过this,this和this的帖子,但是我不知道为什么quotechar
不能在pd.read_csv()
(Python3,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", "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
谢谢你!!
发布于 2016-05-06 23:38:20
read_csv()
中分隔符的Pandas doc
长度大于1个字符且不同于“\s+”的python分隔符将被解释为正则表达式,将强制使用
解析引擎,并将忽略数据中的引号。
尝试使用此命令(默认情况下sep
设置为逗号):
pd.read_csv(file, skipinitialspace = True, quotechar = '"')
发布于 2018-09-18 23:39:33
另一种解决方案是使用适当的正则表达式,而不是简单的\s+
。我们需要查找不在引号内的逗号(,
):
pd.read_csv(file,
sep=', (?=(?:"[^"]*?(?: [^"]*)*))|, (?=[^",]+(?:,|$))',
engine='python')
该表达式取自here。
https://stackoverflow.com/questions/37074914
复制相似问题