2.7 CSV模块需要unicode,但不需要unicode [英] 2.7 CSV module wants unicode, but doesn't want unicode

查看:152
本文介绍了2.7 CSV模块需要unicode,但不需要unicode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

csvfile_ = open(finishedFileName+num+".csv","w",newline='')
writ = csv.writer(csvfile_, dialect='excel')
firstline = unicode(str(firstline))
try:
    writ.writerow(firstline)
except TypeError:
    print firstline
    print type(firstline)
    raise

使用此代码我得到一个TypeError: must be unicode, not str.当打印第一行的类型时,我看到<type 'unicode'>.当我打印第一行时,我看到['project_number', 'project_location'](列表比这更长,但是以这种样式继续.)

I get a TypeError: must be unicode, not str with this code. When printing the type of firstline, I see <type 'unicode'>. When I print firstline, I see ['project_number', 'project_location'](The list is longer than that, but it continues in that style.)

该程序在python 3.3中运行良好.我使用3to2进行了移植,并从Unix切换到Windows.

This program was working fine in python 3.3. I ported it over with 3to2, switching from unix to windows as I did so.

如何使该程序编写流畅?

How do I make this program write smoothly?

注意:根据官方文档,此版本的csv模块不支持Unicode输入,但它告诉我无论如何都要提供Unicode输入.

Note: This version of the csv module doesn’t support Unicode input according to the official documentation, but it told me to give it Unicode input anyway.

完全例外

Traceback (most recent call last):
  File "C:\Users\urightswt\Downloads\LogModToConvert.py", line 382, in <module>
    process(marketingLogExportFileName)
  File "C:\Users\urightswt\Downloads\LogModToConvert.py", line 123, in process
    writing(csvfile,modified,firstline)
  File "C:\Users\urightswt\Downloads\LogModToConvert.py", line 114, in writing
    writ.writerow(firstline)
TypeError: must be unicode, not str

如果我取出用于制作第一行unicode的代码,我会得到

If I take out the code to make firstline unicode, I instead get

Traceback (most recent call last):
  File "C:\Users\urightswt\Downloads\LogModToConvert.py", line 382, in <module>
    process(marketingLogExportFileName)
  File "C:\Users\urightswt\Downloads\LogModToConvert.py", line 123, in process
    writing(csvfile_,modified,firstline)
  File "C:\Users\urightswt\Downloads\LogModToConvert.py", line 114, in writing
    writ.writerow(firstline)
TypeError: must be unicode, not str

推荐答案

不幸的是,3to2使用了io.open()调用,而不是内置的Python 2 open()函数.这会以文本模式打开文件,就像在Python 3上一样,需要Unicode输入.

Unfortunately, 3to2 used the io.open() call instead of the built-in Python 2 open() function. This opened the file in text mode, which like on Python 3 expects Unicode input.

但是, csv模块不支持Unicode数据.它肯定不会产生Unicode.

However, the csv module does not support Unicode data; it certainly does not produce Unicode.

您将不得不在Python 2上以二进制模式打开文件.

You'll either have to open the file in binary mode on Python 2:

mode = 'w'
if sys.version_info.major < 3:
    mode += 'b'
csvfile_ = open(finishedFileName + num + ".csv", mode, newline='')

或改用内置的open()调用:

csvfile_ = open(finishedFileName + num + ".csv", 'wb')

无论如何都必须使用'wb'作为模式.

where you have to use 'wb' as the mode anyway.

如果您要写出unicode数据,则必须在 传递给csv.writer()对象之前对该数据进行编码. csv模块示例部分包含用于从Unicode进行编码的代码写起来容易些.

If you are trying to write out unicode data, you'll have to encode that data before passing it to the csv.writer() object. The csv module examples section includes code to make encoding from Unicode before writing a little easier.

这篇关于2.7 CSV模块需要unicode,但不需要unicode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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