熊猫csv阅读器无法识别分界仪

时间:2017-07-13 18:27:25

标签: python csv pandas

这是csv文件的一个片段:

"tac,""vendor"",""platform"",""type"""
"00100429,""PROTO"",""Proprietary"",""Phone"""
"00100430,""PROTO"",""Proprietary"",""Phone"""
"00100431,""PROTO"",""Proprietary"",""Phone"""

我用以下内容阅读:

pd.read_csv("path/name.csv", sep=",")

但它会返回一个包含一个合并列的数据框:

    tac,"vendor","platform","type"
0   00100429,"PROTO","Proprietary","Phone"
1   00100430,"PROTO","Proprietary","Phone"
2   00100431,"PROTO","Proprietary","Phone"
3   00100432,"PROTO","Proprietary","Phone"
4   00100433,"PROTO","Proprietary","Phone"
5   00100434,"PROTO","Proprietary","Phone"
6   00100435,"PROTO","Proprietary","Phone

显然,我需要将所有文件分隔为“,”

1 个答案:

答案 0 :(得分:3)

使用其他quotechar

pd.read_csv("path/name.csv", quotechar="'") \
    .replace('"', '', regex=True).rename(columns=lambda x: x.strip('"'))

        tac vendor     platform   type
0  00100429  PROTO  Proprietary  Phone
1  00100430  PROTO  Proprietary  Phone
2  00100431  PROTO  Proprietary  Phone
​