我目前正在尝试将一段代码从 python 2 转换为 python 3,但找不到与 unicode
等效的 python 3:
class NavigableString(unicode_literals, PageElement):
def toEncoding(self, s, encoding=None):
"""Encodes an object to a string in some encoding, or to Unicode.
."""
if isinstance(s, unichr()):
if encoding:
s = s.encode(encoding)
elif isinstance(s, str):
if encoding:
s = s.encode(encoding)
else:
s = unicode_literals(s)
else:
if encoding:
s = self.toEncoding(str(s), encoding)
else:
s = unicode_literals(s)
return s
最佳答案
Python 3 中的
常规字符串是 unicode。您必须在 Python 3 中不遗余力地获取字节串(非 Unicode)。
关于python - unicode python 3 等效项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35185378/