问题描述
我尝试将 Python 3 程序向后移植到 2.7,但遇到了一个奇怪的问题:
>>>导入 io>>>导入 csv>>>输出 = io.StringIO()>>>output.write("Hello!") # 失败:io.StringIO 需要 Unicode回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中类型错误:需要 unicode 参数,得到str">>>output.write(u"Hello!") # 这按预期工作.6L>>>writer = csv.writer(output) # 现在让我们用 csv 模块试试这个:>>>csvdata = [u"Hello", u"Goodbye"] # 看,全是Unicode!(?)>>>writer.writerow(csvdata) # 遗憾的是,没有.回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中类型错误:需要 unicode 参数,得到str"根据文档,io.StringIO()
返回 Unicode 文本的内存中流.当我尝试手动为其提供 Unicode 字符串时,它可以正常工作.为什么它与 csv
模块一起失败,即使写入的所有字符串都是 Unicode 字符串?导致异常的 str
来自哪里?
Python 2.7 csv
模块不支持 Unicode 输入:请参阅 文档开头的注释.
看来您必须将 Unicode 字符串编码为字节字符串,并使用 io.BytesIO
而不是 io.StringIO
.
文档的 examples 部分包括 UnicodeReader
和 UnicodeWriter
包装类(感谢@AlexeyKachayev 提供指针).
I tried to backport a Python 3 program to 2.7, and I'm stuck with a strange problem:
>>> import io
>>> import csv
>>> output = io.StringIO()
>>> output.write("Hello!") # Fail: io.StringIO expects Unicode
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unicode argument expected, got 'str'
>>> output.write(u"Hello!") # This works as expected.
6L
>>> writer = csv.writer(output) # Now let's try this with the csv module:
>>> csvdata = [u"Hello", u"Goodbye"] # Look ma, all Unicode! (?)
>>> writer.writerow(csvdata) # Sadly, no.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unicode argument expected, got 'str'
According to the docs, io.StringIO()
returns an in-memory stream for Unicode text. It works correctly when I try and feed it a Unicode string manually. Why does it fail in conjunction with the csv
module, even if all the strings being written are Unicode strings? Where does the str
come from that causes the Exception?
The Python 2.7 csv
module doesn't support Unicode input: see the note at the beginning of the documentation.
It seems that you'll have to encode the Unicode strings to byte strings, and use io.BytesIO
, instead of io.StringIO
.
The examples section of the documentation includes examples for a UnicodeReader
and UnicodeWriter
wrapper classes (thanks @AlexeyKachayev for the pointer).
这篇关于如何将 io.StringIO() 与 csv 模块一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!