NameError:全局名称“ unicode”未定义-在Python 3中
我正在尝试使用一个名为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软件包,请让我知道他们是否发现了类似的问题。我感谢您的帮助。
-
Python 3将
unicode
类型重命名为str
,旧str
类型已替换为bytes
。if isinstance(unicode_or_str, str): text = unicode_or_str decoded = False else: text = unicode_or_str.decode(encoding) decoded = True
您可能需要阅读Python 3 porting
HOWTO以获得更多此类详细信息。Lennart
Regebro的Porting to Python 3:深入指南,可免费在线获得。最后但并非最不重要的一点是,您可以尝试使用该
2to3
工具查看如何为您转换代码。