简体   繁体   English

如何读取csv文件,该文件在逗号旁边带有双引号

[英]How to read a csv file, which has double quotes next to commas

I have a csv file that its rows look like this 我有一个csv文件,其行如下所示

q4_1,"blabla,bla",new_label q4_2,alb,new_label2

and I would like to read it in a pandas data.frame that will look like this 我想在看起来像这样的pandas data.frame中阅读它

import pandas as pd

pd.DataFrame({'col1' : ['q4_1','q4_2'],
              'col2' : ['blabla,bla','alb'],
              'col3' : ['new_label','new_label2']})

Which parameter in the pd.read_csv function should I use for that ? 我应该pd.read_csv使用pd.read_csv函数中的哪个参数? I couldn't figure it out from the documentation 我无法从文档中弄清楚

With python 3.7 and pandas 0.23.4 , worked this: 使用python 3.7pandas 0.23.4 ,可以做到这一点:

import pandas as pd

df0 = pd.read_csv('data.csv', names=["col1", "col2", "col3"], squeeze=True)

the result is a DataFrame with the structure described in the question: 结果是具有问题中描述的结构的DataFrame:

    col1        col2        col3
 0  q4_1  blabla,bla   new_label
 1  q4_2         alb  new_label2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM