pandas to_csv 输出引用问题 [英] pandas to_csv output quoting issue

查看:28
本文介绍了pandas to_csv 输出引用问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在获取 Pandas dataframe.to_csv(...) 输出引用字符串时遇到问题.

将pandas导入为pdtext = '这是输出文本"'df = pd.DataFrame(index=['1'],columns=['1','2'])df.loc['1','1']=123df.loc['1','2']=文本df.to_csv('foo.txt',index=False,header=False)

输出为:

<块引用>

123,"这是""输出文本"""

但我想:

<块引用>

123,这是输出文本"

有谁知道如何做到这一点?

解决方案

你可以通过quoting=csv.QUOTE_NONE,例如:

<预><代码>>>>df.to_csv('foo.txt',index=False,header=False)>>>!cat foo.txt123,"这是""输出文本""">>>导入 csv>>>df.to_csv('foo.txt',index=False,header=False, quoting=csv.QUOTE_NONE)>>>!cat foo.txt123,这是外文"

但根据我的经验,引用更多而不是更少更好.

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?

解决方案

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.

这篇关于pandas to_csv 输出引用问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆