【发布时间】:2019-04-29 14:52:47
【问题描述】:
我如何翻译 unicode(s, "utf-8") 以同时在 Python 2 和 Python 3 中工作?
unicode() 在 Python 3 中被移除,因为所有 str 都是 Unicode,但 str() 不像 那样采用第二个参数>unicode() 这样做,它不是一个有效的替代品。
我试过了:
>>> for s in ("Luesai", u"Lüsai"):
... print(s)
... a = unicode(s, "utf-8")
... print(a)
... b = unicode(s).encode("utf-8")
... print(b)
... print(a == b)
...
Luesai
Luesai
Luesai
True
Lüsai
TypeError: decoding Unicode is not supported
【问题讨论】:
-
您的测试程序基于许多在 Python 3 中不成立的假设。即使您得到满意的答案,也需要重做。
标签: python python-3.x unicode porting