简体   繁体   English

如何阻止 Pandas 从 csv 文件中删除封闭字符?

[英]How to stop Pandas from removing enclosing characters from a csv file?

I had to trim a very large csv file and I decided to use pandas for it.我不得不修剪一个非常大的 csv 文件,我决定使用 pandas 。 The file is a CSV and every value in the CSV is enclosed by double quotes.该文件是 CSV 并且 CSV 中的每个值都用双引号引起来。 So the file looked like所以文件看起来像

"Col1", "Col2", "Col3"
"foo", "fii", "fuu"

However, upon using df = pd.read_csv(path, encoding='Latin1', low_memory=False) all of the double quotes get removed, and after trimming the file and exporting it with df.to_csv to a new csv, the new csv has但是,在使用df = pd.read_csv(path, encoding='Latin1', low_memory=False)时,所有双引号都会被删除,并且在修剪文件并使用df.to_csv将其导出到新的 csv 后,新的 csv有

col1, col2, col3
foo, fii, fuu

With no enclosing characters.没有封闭字符。 I definitely need those double quotes, is there a way to preserve them or to add them again?我绝对需要那些双引号,有没有办法保留它们或再次添加它们?

solution解决方案

https://docs.python.org/3/library/csv.html#csv.QUOTE_ALL https://docs.python.org/3/library/csv.html#csv.QUOTE_ALL

import csv
df.to_csv('out.csv',header=True, quoting=csv.QUOTE_ALL)

It looks like you can read csv with quotechar option set to something else:看起来您可以阅读 csv 并将quotechar选项设置为其他内容:

from io import StringIO

s = '''"Col1", "Col2", "Col3"
"foo", "fii", "fuu"'''

pd.read_csv(StringIO(s), quotechar="'")

Output: Output:

  "Col1"  "Col2"  "Col3"
0  "foo"   "fii"   "fuu"

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

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