Pandas Dataframe CSV 导出,如何防止额外的双引号字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26186033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas Dataframe CSV export, how to prevent additional double-quote characters
提问by rdh9
I am using Pandas to process and output data for a table which is published in Wordpress
我正在使用 Pandas 处理和输出在 Wordpress 中发布的表的数据
I am adding HTML code to format color one column
我正在添加 HTML 代码来格式化一列的颜色
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:
我将数据导出为 csv 文件,因为我需要逗号分隔符:
output = r'C:\test\test.csv'
df.to_csv(output, index=False)
If I open the csv in Excel, it looks exactly as above
如果我在 Excel 中打开 csv,它看起来与上面完全一样
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.
任何帮助表示赞赏。
回答by Rob?
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
- 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>

