NameError: globalna nazwa 'unicode' nie jest zdefiniowana - w Pythonie 3

Próbuję użyć pakietu Pythona o nazwie bidi. W module w tym pakiecie (algorithm.py) są pewne linie, które dają mi błąd, chociaż jest to część pakietu.

Oto linie:

# 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

A oto komunikat o błędzie:

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

Jak napisać tę część kodu, aby działała w Python3? Również jeśli ktoś korzystał z pakietu bidi z Pythonem 3, proszę dać mi znać, czy znalazł podobne problemy, czy nie. Doceniam twoją pomocy.

Author: Martijn Pieters, 2013-11-09

5 answers

Python 3 zmienił nazwę typu unicode na str, stary typ str został zastąpiony przez bytes.

if isinstance(unicode_or_str, str):
    text = unicode_or_str
    decoded = False
else:
    text = unicode_or_str.decode(encoding)
    decoded = True

Możesz przeczytać HOWTO portowania Pythona 3, aby uzyskać więcej takich szczegółów. Istnieje również 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 Narzędzie , aby zobaczyć, jak to tłumaczy kod dla Ciebie.

 238
Author: Martijn Pieters,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-11-09 15:07:56

Jeśli potrzebujesz, aby skrypt działał dalej na python2 i 3, tak jak ja, może to komuś pomóc

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

I może wtedy po prostu zrobić na przykład

foo = unicode.lower(foo)
 33
Author: Neil McGill,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-11-13 17:36:22

Możesz użyć biblioteki six do obsługi Pythona 2 i 3:

import six
if isinstance(value, six.string_types):
    handle_string(value)
 23
Author: atm,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-12 20:22:38

Mam nadzieję, że używasz Pythona 3 , Str są domyślnie unicode, więc proszę Zastąp funkcję Unicode ciągiem znaków Str function.

if isinstance(unicode_or_str, str):    ##Replaces with str
    text = unicode_or_str
    decoded = False
 1
Author: M.J,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-27 09:54:10

Można zastąpić unicode przez u''.__class__, aby obsłużyć brakującą klasę unicode w Pythonie 3. Zarówno dla Pythona 2 jak i 3, możesz użyć konstrukcji

isinstance(unicode_or_str, u''.__class__)

Lub

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

W zależności od dalszego przetwarzania, rozważ inny wynik:

Python 3

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

Python 2

>>> isinstance(u'text', u''.__class__)
True
>>> isinstance('text', u''.__class__)
False
 1
Author: Friedrich,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-11-25 10:55:47