python unicode is not defined_NameError:全局名称“ unicode”未定义-在Python 3中

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.

Here are the lines:

# 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

and here is the error message:

Traceback (most recent call last):

File "", line 1, in

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

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 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

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.

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值