Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Dataframe CSV export, how to prevent additional double-quote characters

Tags:

python

pandas

csv

I am using Pandas to process and output data for a table which is published in Wordpress

I am adding HTML code to format color one column

Starting with a sample Dataframe:

import numpy as np
import pandas as pd
df = pd.DataFrame({
                'A': ['group1', 'group2', 'group3'],
                'B': ['foo', 'foo', 'foo'] })

print df

        A    B
0  group1  foo
1  group2  foo
2  group3  foo

I then add the same formatting code to each row like this:

df['Status'] = '<span style="color: #00CD00">Active</span>'

print df    

        A    B                                      Status
0  group1  foo  <span style="color: #00CD00">Active</span>
1  group2  foo  <span style="color: #00CD00">Active</span>
2  group3  foo  <span style="color: #00CD00">Active</span>

I export the data as a csv file, because I need the comma delimiters:

output = r'C:\test\test.csv'
df.to_csv(output, index=False)

If I open the csv in Excel, it looks exactly as above

But if I open it in a text editor (which I need to do to get the delimiters), I find the column with the formatting string has additional doublequote characters, like this:

"<span style=""color: #00CD00"">Active</span>"

-- this is without the added doublequotes -- which would be correct:

<span style="color: #00CD00">Active</span>

Does anybody know how I can export this without the additional characters?

Any help appreciated.

like image 204
rdh9 Avatar asked Oct 03 '14 20:10

rdh9


1 Answers

df.to_csv('test.csv', index=False, quoting=csv.QUOTE_NONE)

References:

  • http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_csv.html
  • https://docs.python.org/2/library/csv.html#csv.QUOTE_NONE

Sample Program:

import numpy as np
import pandas as pd
import csv
df = pd.DataFrame({
                'A': ['group1', 'group2', 'group3'],
                'B': ['foo', 'foo', 'foo'] })
df['Status'] = '<span style="color: #00CD00">Active</span>'
df.to_csv('test.csv', index=False, quoting=csv.QUOTE_NONE)

Result:

$ cat test.csv
A,B,Status
group1,foo,<span style="color: #00CD00">Active</span>
group2,foo,<span style="color: #00CD00">Active</span>
group3,foo,<span style="color: #00CD00">Active</span>
like image 67
Robᵩ Avatar answered Oct 07 '22 00:10

Robᵩ