Pandas Export CSV Dataframe Files How to Prevent Extra Double Quotes

I use Pandas to process and output data for a table that is published in Wordpress

I am adding HTML to format the color of a single 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

  AB 0 group1 foo 1 group2 foo 2 group3 foo 

Then I add the same formatting code to each line as follows:

 df['Status'] = '<span style="color: #00CD00">Active</span>' print df AB 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 to a csv file because I need comma delimiters:

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

If I open csv in Excel, it will look the same as above

But if I open it in a text editor (which I need to do to get the delimiters), I find that the column with the format string contains additional double-letter characters, for example:

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

- this is without the added double queries - which would be correct:

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

Does anyone know how I can export this without extra characters?

Any help was appreciated.

+7
python pandas csv
source share
1 answer
 df.to_csv('test.csv', index=False, quoting=csv.QUOTE_NONE) 

Literature:

Program Example:

 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> 
+9
source share

All Articles