gpt4 book ai didi

python pandas read_csv quotechar 不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 12:23:41 24 4
gpt4 key购买 nike

我读过 this , thisthis帖子,但尽管我不知道为什么 quotecharpd.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", "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() 中的分隔符:

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.

尝试改用它(sep 默认设置为逗号):

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

关于python pandas read_csv quotechar 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37074914/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com