我正在尝试使用一个名为 bidi 的 Python 包。在这个包 (algorithm.py) 的一个模块中,有一些行给我错误,尽管它是包的一部分。
这是几行:
# utf-8 ? we need unicode
if isinstance(unicode_or_str, unicode):
text = unicode_or_str
decoded = False
else:
text = unicode_or_str.decode(encoding)
decoded = True
这是错误消息:
Traceback (most recent call last):
File "<pyshell#25>", line 1, in <module>
bidi_text = get_display(reshaped_text)
File "C:\Python33\lib\site-packages\python_bidi-0.3.4-py3.3.egg\bidi\algorithm.py", line 602, in get_display
if isinstance(unicode_or_str, unicode):
NameError: global name 'unicode' is not defined
我应该如何重写这部分代码以便它在 Python3 中工作?另外,如果有人在 Python 3 中使用过 bidi 包,请告诉我他们是否发现了类似的问题。我感谢您的帮助。
原文由 TJ1 发布,翻译遵循 CC BY-SA 4.0 许可协议
Python 3 renamed the
unicode
type tostr
, the oldstr
type has been replaced bybytes
.您可能需要阅读 Python 3 移植 HOWTO 以获得更多此类详细信息。还有 Lennart Regebro 的 Porting to Python 3: An in-depth guide ,在线免费。
最后但同样重要的是,您可以尝试使用
2to3
工具 来查看它如何为您翻译代码。