在熊猫中导出.csv的区别-双引号

时间:2020-01-26 11:00:37

标签: python excel pandas csv

我有一个.csv文件,以这种方式显示(当我在excel中打开文件时)

a,description,b,c                                #columns
something,"bla,bla,bla",something,something      #elements
something,"bla,bla,bla",something,something      #elements
something,"bla,bla,bla",something,something      #elements

当我在熊猫中导入.csv时

df = pd.read_csv("a.csv")

描述值不带双引号

       a       description     b          c            
0   something  bla,bla,bla  something  something      
1   something  bla,bla,bla  something  something      
2   something  bla,bla,bla  something  something

第一种情况

现在,如果我将df 照原样从熊猫导出到.csv df.to_csv(r"a1.csv", index=False, header=True),则双引号仍会按预期出现在.csv文件中(当我在excel中打开时) )

a,description,b,c                                #columns
something,"bla,bla,bla",something,something      #elements
something,"bla,bla,bla",something,something      #elements
something,"bla,bla,bla",something,something      #elements

第二种情况

但是如果我通过在熊猫中屏蔽来减少数据集

df1 = df.loc[:,("description","c")]


      description     c                      
0     bla,bla,bla  something       
1     bla,bla,bla  something       
2     bla,bla,bla  something 

,然后使用同一行代码df1.to_csv(r"a2.csv", index=False, header=True)将此新数据集df1导出到.csv文件,而无需设置quoting=csv.QUOTE_NONE,双引号现在在.csv中消失了(当我在excel)

description,c              #columns
bla,bla,bla,something      #elements
bla,bla,bla,something      #elements
bla,bla,bla,something      #elements

我想知道为什么会这样吗? 而且我还想知道,如果我将最后一个.csv导入到熊猫error: Expected 2 fields in line x, saw x中,为什么“描述”列中存在的所有逗号都不会引发错误df2 = pd.read_csv("a2.csv")。 似乎即使没有显示双引号也可以读取并显示

0 个答案:

没有答案