【问题标题】:NameError: global name 'unicode' is not defined - in Python 3NameError:全局名称 \'unicode\' 未定义 - 在 Python 3 中
【发布时间】:2022-02-24 10:24:35
【问题描述】:

我正在尝试使用一个名为 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) 应该可以。重命名变量名称是可选的。
    【解决方案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'')
      

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

      蟒蛇3

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

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

        【讨论】:

          【解决方案7】:

          你可以在 python2 或 python3 中使用它

          type(value).__name__ == 'unicode':
          

          【讨论】:

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