简体   繁体   English

CSV:什么时候unicode不是unicode?

[英]CSV: when unicode is not unicode?

Why writerow() says I'm passing str , when I'm passing unicode ? 为什么writerow()说我在传递unicode时传递了str

import io
import csv

with io.open('test.csv', 'w', encoding="utf-8") as f:
    writer = csv.writer(f)
    x = [unicode(v, 'utf8') for v in ['id:ID', 'pos:string', 'definition:string', ':LABEL']]
    print x
    print type(x[0])
    writer.writerow(x)

[u'id:ID', u'pos:string', u'definition:string', u':LABEL']
<type 'unicode'>
Traceback (most recent call last):
 File "testcsv.py", line 9, in <module>
     writer.writerow(x)
  TypeError: write() argument 1 must be unicode, not str

The csv module in python 2 does not handle unicode . python 2中的csv模块不处理unicode The python 2 documentation gives an example of how to create your own unicode csv handlers instead. python 2文档提供了有关如何创建自己的unicode csv处理程序的示例

A better option would be to install the backports.csv module, which allows your python 2 code to use the newer python 3 csv api, which handles unicode. 更好的选择是安装backports.csv模块,该模块允许您的python 2代码使用更新的python 3 csv api(可处理unicode)。

After installing the library using pip install backports.csv , this code works in python 2: 使用pip install backports.csv安装库之后,此代码可在python 2中运行:

>>> import io
>>> from backports import csv
>>> with io.open('test.csv', 'w', encoding="utf-8") as f:
>>>     writer = csv.writer(f)
>>>     x = [unicode(v, 'utf8') for v in ['id:ID', 'pos:string', 'definition:string', ':LABEL']]
>>>     print x
>>>     print type(x[0])
>>>     writer.writerow(x)
[u'id:ID', u'pos:string', u'definition:string', u':LABEL']
<type 'unicode'>

>>> with io.open('test.csv', encoding="utf-8") as f:
>>>     print f.read()
id:ID,pos:string,definition:string,:LABEL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM