Как использовать slugify в Python 3?Python

Программы на Python
Anonymous
Как использовать slugify в Python 3?

Сообщение Anonymous »

Я пытаюсь использовать slugify, который я установил с помощью pip3 install slugify. Однако в интерпретаторе, если я попытаюсь преобразовать строку «привет», я увижу следующее:

Код: Выделить всё

Python 3.5.2 (default, Nov 17 2016, 17:05:23)
Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: from slugify import slugify

In [2]: slugify('hello')
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
 in ()
----> 1 slugify('hello')

/usr/local/lib/python3.5/dist-packages/slugify.py in slugify(string)
22
23     return re.sub(r'[-\s]+', '-',
---> 24             unicode(
25                 re.sub(r'[^\w\s-]', '',
26                     unicodedata.normalize('NFKD', string)

NameError: name 'unicode' is not defined

In [3]: slugify(u'hello')
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
 in ()
----> 1 slugify(u'hello')

/usr/local/lib/python3.5/dist-packages/slugify.py in slugify(string)
22
23     return re.sub(r'[-\s]+', '-',
---> 24             unicode(
25                 re.sub(r'[^\w\s-]', '',
26                     unicodedata.normalize('NFKD', string)

NameError: name 'unicode' is not defined
Напротив, в Python 2 последнее работает:

Код: Выделить всё

Python 2.7.12 (default, Nov 19 2016, 06:48:10)
Type "copyright", "credits" or "license" for more information.

IPython 2.4.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: from slugify import slugify

In [2]: slugify(u'hello')
Out[2]: u'hello'
Как заставить это работать в Python 3?

Вернуться в «Python»