- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在尝试使用名为 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/
虽然我试图完全理解 NoMethodError 和 NameError 之间的区别,但我发现 NameError 在祖先中不在 0 位置。是 DidYouMean 的魔法吗? [1] pry(main
请谁来帮我解决这个问题。**当在服务器上运行时,特定的结果会出现,但服务器运行得很好。当我运行代码时,它发送这个错误
下面的 Python 由于某种原因失败了。 class NetVend: def blankCallback(data): pass def sendCommand(c
作为this great answer建议,从 Python 3.7 开始,如果 from __future__ import annotations 使用指令。 但是,如果我想为注释类型创建别名,这
我在导入自制模块时遇到了一些麻烦,我只是看不出我做错了什么。 我有一个名为 basics 的包,其中包含我所有的基类 我有一个名为 components 的第二个包,components 中的每个模块
我的 python 代码不断出现名称错误,未在 ticketSold 上定义全局变量。我不确定如何解决这个问题,因为我确实将其定义为全局变量。感谢您的帮助。 aLimit=300 bLimit=500
我正在安装 Redmine,但由于抽成问题,我快疯了。特别是,为了对不同的 Ruby 插件执行捆绑安装,我被要求安装 xapian-full-alaveteli,v 1.2.9.5。问题是我收到以下错
我的 python 代码不断出现名称错误,未在 ticketSold 上定义全局变量。我不确定如何解决这个问题,因为我确实将其定义为全局变量。感谢您的帮助。 aLimit=300 bLimit=500
我正在安装 Redmine,但由于抽成问题,我快疯了。特别是,为了对不同的 Ruby 插件执行捆绑安装,我被要求安装 xapian-full-alaveteli,v 1.2.9.5。问题是我收到以下错
所以我是第一年,有一点编码经验,但不多,我被赋予了制作基于文本的冒险游戏的任务。我的讲师给了我们一个模板并帮助我们开始,但我很困惑为什么这段代码会导致 shell 显示 NameError: name
我有一些编程经验,但我对 python 很陌生,我正在尝试弄清楚如何使用和导入 .py 文件中的类而不是 main 。我目前正在使用 netbeans,运行 CPython 3.2.1。 根据我现在的
代码非常简单,我刚刚开始用 python 编程 代码 man = input ("what's your name mister") print("his name is "+man) 运行程序后收到
我在文件中创建一个类。声明一些类变量 A = 5 和另一个类变量 B = A+1。从另一个文件导入该类时,出现 NameError: name 'A' is not Defined。有没有办法解决这个
我一直在为 Minecraft 的 Raspberry Pi 版本制作 mod,每次在程序中输入其中一个命令时,我都会遇到非常令人沮丧的错误。这是我的代码: import minecraft.mine
为什么会这样: def fn(proc, *args, **kwargs): cache = proc.cache = {} def cached_execution(cache, *
我是 Python 的新手,正在学习类,正在尝试编写一个“个人信息”程序: 这是我的代码: class PersonalInfo(): def names(self, name):
我今天用这个简单的代码遇到了一个非常奇怪的问题: var1 = 1 var2 = 2 if var1 > var2 > var3: print('Does not run') print('D
我正在 try catch 无法加载模块时发生的任何异常。当前的结果是“except” block 没有被执行。 import sys def loadModule(module): try:
这是我的代码: import os if os.path.exists(r'C:\Genisis_AI'): print("Main File path exists! Continuing
我正在学习列表理解,通过切换变量我得到了 2 个不同的结果,尽管它们看起来应该工作相同。 数组 a 等于 [[0, 0, 0, 0, 0], [1, 0, 0, 0, 0], [0, 0, 0, 0
我是一名优秀的程序员,十分优秀!