如何将unicode(s,“utf8”)转换为在python2和python3中工作?

2024-10-01 17:23:35 发布

您现在位置:Python中文网/ 问答频道 /正文

如何翻译unicode(s,“utf-8”)以在python2和python3中工作?你知道吗

在Python3中删除了unicode(),因为所有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

Tags: intrue替代品for参数unicodepython3utf

热门问题