Python Pandas to_csv 输出返回字符串/对象值的单个字符 [英] Python Pandas to_csv Output Returns Single Character for String/Object Values

查看:112
本文介绍了Python Pandas to_csv 输出返回字符串/对象值的单个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将结果输出到 Pandas 数据框中.当我打印数据框时,对象值显示正确,但是当我在数据框上使用 to_csv 函数时,我的 csv 输出只有每个字符串/对象值的第一个字符.

df = pandas.DataFrame({'a':[u'u\x00s\x00']})df.to_csv('test.csv')

我还尝试对 to_csv 函数添加以下内容:

df.to_csv('test_encoded.csv', encoding='utf-8')

但我得到了相同的结果:

<预><代码>>>>打印文件一种0我们(在 csv 文件中输出)你

作为参考,我正在连接到 Vertica 数据库并使用以下设置:

  • 操作系统:Mac OS X Yosemite (10.10.5)
  • Python 2.7.10 |Anaconda 2.3.0 (x86_64)|(默认,2015 年 9 月 15 日,14:29:08)
  • pyodbc 3.0.10
  • 熊猫 0.16.2
  • ODBC:Vertica ODBC 6.1.3

如果您能帮助我们了解如何使用 pandas 中的 to_csv 函数传递整个对象字符串,我们将不胜感激.

解决方案

我遇到了同样的问题,找到了这篇文章 Python 中的 UTF-32

要解决您的问题,我认为您需要将所有 '\x00' 替换为 empty.我设法用下面的代码编写了正确的 CSV

fixer = dict.fromkeys([0x00], u'')df['a'] = df['a'].map(lambda x: x.translate(fixer))df.to_csv('test.csv')

为了解决我的 Vertica 问题,我必须在文件 /Library/Vertica/ODBC/lib/vertica.ini 中将编码更改为 UTF-16下面的配置

[驱动程序]ErrorMessagesPath=/Library/Vertica/ODBC/messages/ODBCInstLib=/usr/lib/libiodbcinst.dylibDriverManagerEncoding=UTF-16

最好的问候,
安德森·内维斯

I'm attempting to output the result into a pandas data frame. When I print the data frame, the object values appear correct, but when I use the to_csv function on the data frame, my csv output has only the first character for every string/object value.

df = pandas.DataFrame({'a':[u'u\x00s\x00']})
df.to_csv('test.csv')

I've also tried the following addition to the to_csv function:

df.to_csv('test_encoded.csv', encoding= 'utf-8')

But am getting the same results:

>>> print df
      a
0  us

(output in csv file)
u

For reference, I'm connecting to a Vertica database and using the following setup:

  • OS: Mac OS X Yosemite (10.10.5)
  • Python 2.7.10 |Anaconda 2.3.0 (x86_64)| (default, Sep 15 2015, 14:29:08)
  • pyodbc 3.0.10
  • pandas 0.16.2
  • ODBC: Vertica ODBC 6.1.3

Any help figuring out how to pass the entire object string using the to_csv function in pandas would be greatly appreciated.

解决方案

I was facing the same problem and found this post UTF-32 in Python

To fix your problem, I believe that you need to replace all '\x00' by empty. I managed to write the correct CSV with the code below

fixer = dict.fromkeys([0x00], u'')
df['a'] = df['a'].map(lambda x: x.translate(fixer))
df.to_csv('test.csv')

To solve my problem with Vertica I had to change the encoding to UTF-16 in the file /Library/Vertica/ODBC/lib/vertica.ini with the configuration below

[Driver]
ErrorMessagesPath=/Library/Vertica/ODBC/messages/
ODBCInstLib=/usr/lib/libiodbcinst.dylib
DriverManagerEncoding=UTF-16

Best regards,
Anderson Neves

这篇关于Python Pandas to_csv 输出返回字符串/对象值的单个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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