如何翻译unicode(s,"utf-8"(以在Python 2和Python 3中工作?
unicode((在Python 3中被删除,因为所有str都是unicode,但tr((不接受第二个参数,就像 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
如果您有一个字节字符串,在2和3中翻译它的方法是使用decode
:
a = s.decode("utf-8")