【问题标题】:NameError: global name 'unicode' is not defined - in Python 3NameError:未定义全局名称“unicode” - 在 Python 3 中
【发布时间】:2013-11-21 12:57:48
【问题描述】:

我正在尝试使用名为 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 unicode python-3.x nameerror bidi


    【解决方案1】:

    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 来查看它如何为您翻译代码。

    【讨论】:

    • 所以我应该写:if isinstance(unicode_or_str, str)? 'unicode_or_str' 怎么样?
    • 变量名在这里无关紧要; if isinstance(unicode_or_str, str) 应该可以工作。重命名变量名是可选的。
    • @TJ1:确保您没有删除右括号或某处的东西。代码应该可以正常使用 just unicode 替换为 str
    • 你是对的 Martijn,我忘记在我的代码中包含 :,感谢您的帮助,它现在可以工作了。
    • 我喜欢2to3工具
    【解决方案2】:

    如果您需要像我一样让脚本继续在 python2 和 3 上运行,这可能会对某人有所帮助

    import sys
    if sys.version_info[0] >= 3:
        unicode = str
    

    然后就可以做例子

    foo = unicode.lower(foo)
    

    【讨论】:

    • 这是正确的想法,很好的答案。只是添加一个细节,如果你使用six 库来管理 Python 2/3 兼容性,你可以这样:if six.PY3: unicode = str 而不是sys.version_info 的东西。这对于防止在 Python 3 中未定义与 unicode 相关的 linter 错误也非常有帮助,无需特殊的 linter 规则豁免。
    【解决方案3】:

    您可以使用six 库来支持 Python 2 和 3:

    import six
    if isinstance(value, six.string_types):
        handle_string(value)
    

    【讨论】:

      【解决方案4】:

      可以将unicode 替换为u''.__class__ 以处理Python 3 中缺少的unicode 类。对于Python 2 和3,您可以使用该构造

      isinstance(unicode_or_str, u''.__class__)
      

      type(unicode_or_str) == type(u'')
      

      根据您的进一步处理,考虑不同的结果:

      Python 3

      >>> isinstance(u'text', u''.__class__)
      True
      >>> isinstance('text', u''.__class__)
      True
      

      Python 2

      >>> isinstance(u'text', u''.__class__)
      True
      >>> isinstance('text', u''.__class__)
      False
      

      【讨论】:

        【解决方案5】:

        希望您使用的是 Python 3, str 默认是 unicode,所以请 将Unicode 函数替换为字符串Str 函数。

        if isinstance(unicode_or_str, str):    ##Replaces with str
            text = unicode_or_str
            decoded = False
        

        【讨论】:

        • 不会像@atm 的答案那样保留 BC 请考虑撤回或更新您的答案。没有理由让 python2 用户落后或破坏 python3
        【解决方案6】:

        如果第 3 方库使用 unicode 并且您无法更改其源代码,则以下猴子补丁可以在模块中使用 str 而不是 unicode

        import <module>
        <module>.unicode = str
        

        【讨论】:

          猜你喜欢
          • 2022-02-24
          • 2013-10-29
          • 2013-08-23
          • 1970-01-01
          • 2012-03-28
          • 2013-07-20
          • 2023-03-30
          相关资源
          最近更新 更多