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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With