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.
df.to_csv('test.csv', index=False, quoting=csv.QUOTE_NONE)
References:
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>
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