gpt4 book ai didi

python - NameError:未定义全局名称 'unicode' - 在 Python 3 中

转载 作者:IT老高 更新时间:2023-10-28 21:07:11 25 4
gpt4 key购买 nike

我正在尝试使用名为 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: An in-depth guide ,免费在线。

最后但同样重要的是,您可以尝试使用 2to3 tool看看它是如何为你翻译代码的。

关于python - NameError:未定义全局名称 'unicode' - 在 Python 3 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19877306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com