NameError:全局名称“unicode"未定义 - 在 Python 3 中 [英] NameError: global name 'unicode' is not defined - in Python 3

查看:24
本文介绍了NameError:全局名称“unicode"未定义 - 在 Python 3 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用名为 bidi 的 Python 包.在这个包中的一个模块 (algorithm.py) 中,有一些行给我错误,尽管它是包的一部分.

I am trying to use a Python package called bidi. In a module in this package (algorithm.py) there are some lines that give me error, although it is part of the package.

这里是几行:

# 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:Python33libsite-packagespython_bidi-0.3.4-py3.3.eggidialgorithm.py",   line 602, in get_display
    if isinstance(unicode_or_str, unicode):
NameError: global name 'unicode' is not defined

我应该如何重写这部分代码以便它在 Python3 中工作?另外,如果有人在 Python 3 中使用过 bidi 包,请告诉我他们是否发现了类似的问题.感谢您的帮助.

How should I re-write this part of the code so it works in Python3? Also if anyone have used bidi package with Python 3 please let me know if they have found similar problems or not. I appreciate your help.

推荐答案

Python 3 将 unicode 类型重命名为 str,旧的 str类型已被替换为 bytes.

Python 3 renamed the unicode type to str, the old str type has been replaced by bytes.

if isinstance(unicode_or_str, str):
    text = unicode_or_str
    decoded = False
else:
    text = unicode_or_str.decode(encoding)
    decoded = True

您可能需要阅读 Python 3 移植 HOWTO 以了解更多此类详细信息.还有 Lennart Regebro 的移植到 Python 3:深入指南,在线免费.

You may want to read the Python 3 porting HOWTO for more such details. There is also Lennart Regebro's Porting to Python 3: An in-depth guide, free online.

最后但并非最不重要的一点,您可以尝试使用 2to3 工具 看看它如何为您翻译代码.

Last but not least, you could just try to use the 2to3 tool to see how that translates the code for you.

这篇关于NameError:全局名称“unicode"未定义 - 在 Python 3 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆