Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas to_csv output quoting issue

I'm having trouble getting the pandas dataframe.to_csv(...) output quoting strings right.

import pandas as pd  text = 'this is "out text"' df = pd.DataFrame(index=['1'],columns=['1','2']) df.loc['1','1']=123 df.loc['1','2']=text df.to_csv('foo.txt',index=False,header=False) 

The output is:

123,"this is ""out text"""

But I would like:

123,this is "out text"

Does anyone know how to get this right?

like image 964
user3199761 Avatar asked Jan 15 '14 20:01

user3199761


People also ask

What happens when we use PD to_csv ()?

Pandas DataFrame to_csv() function exports the DataFrame to CSV format. If a file argument is provided, the output will be the CSV file. Otherwise, the return value is a CSV format like string. sep: Specify a custom delimiter for the CSV output, the default is a comma.

Does to_csv overwrite?

If the file already exists, it will be overwritten. If no path is given, then the Frame will be serialized into a string, and that string will be returned.

What does to_csv return?

to_csv() function write the given series object to a comma-separated values (csv) file/format. Parameter : path_or_buf : File path or object, if None is provided the result is returned as a string.

How do I convert a pandas DataFrame to a CSV file?

By using pandas. DataFrame. to_csv() method you can write/save/export a pandas DataFrame to CSV File. By default to_csv() method export DataFrame to a CSV file with comma delimiter and row index as the first column.


1 Answers

You could pass quoting=csv.QUOTE_NONE, for example:

>>> df.to_csv('foo.txt',index=False,header=False) >>> !cat foo.txt 123,"this is ""out text""" >>> import csv >>> df.to_csv('foo.txt',index=False,header=False, quoting=csv.QUOTE_NONE) >>> !cat foo.txt 123,this is "out text" 

but in my experience it's better to quote more, rather than less.

like image 115
DSM Avatar answered Nov 05 '22 18:11

DSM