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

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

问题描述

我对python熊猫相当陌生,但是很难得到to_csv的输出引用。

  import pandas as pd 

text ='这是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)

输出结果是:


<123>,this isout text


但是我想:


<123>,这是out text



有谁知道如何得到这个权利?您可以通过 quoting = csv.QUOTE_NONE 来解决这个问题。

解决方案 ,例如:

 >>> df.to_csv('foo.txt',index = False,header = False)
>>> !cat foo.txt
123,this isout text
>>> import csv
>>> df.to_csv('foo.txt',index = False,header = False,quoting = csv.QUOTE_NONE)
>>> !cat foo.txt
123,这是out text

但以我的经验引用更多,而不是更少。


I am fairly new to python pandas, but having trouble getting the to_csv output quoting 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? Thanks in advance.

解决方案

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天全站免登陆