【发布时间】:2011-03-03 14:09:20
【问题描述】:
以下代码在 Python 3.x 中使用 TypeError: must be str, not bytes 失败,因为现在 encode() 返回 bytes 和 print() 只需要 str。
#!/usr/bin/python
from __future__ import print_function
str2 = "some unicode text"
print(str2.encode('raw_unicode_escape'))
如何使用print() 打印Unicode 字符串转义表示?我正在寻找适用于 Python 2.6 或更高版本(包括 3.x)的解决方案
更新
下面的行将适用于 3.x,但不适用于 2.6,生成 AttributeError: 'file' object has no attribute 'buffer'
sys.stdout.buffer.write(str2.encode('raw_unicode_escape'))
【问题讨论】:
标签: python unicode python-3.x