NameError: global name ‘unicode‘ is not defined - in Python 3 错误解决

在Python3环境下使用bidi包时遇到错误,原因是旧代码中的'unicode'未被定义。解决方案是将`unicode`替换为`str`,因为Python3中unicode类型已更名为str。此外,可以参考Python3的移植指南或使用2to3工具进行代码转换。
摘要由CSDN通过智能技术生成

我正在尝试使用一个名为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

解决办法:
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工具查看如何为您转换代码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值