【问题标题】:How to use slugify in Python 3?如何在 Python 3 中使用 slugify?
【发布时间】:2017-10-19 20:34:46
【问题描述】:

我正在尝试使用slugify,我使用pip3 install slugify 安装了它。但是,在解释器中,如果我尝试对字符串 'hello' 进行 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)
<ipython-input-2-a58110f37579> in <module>()
----> 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)
<ipython-input-3-acc9f7b8d41e> in <module>()
----> 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 slugify


    【解决方案1】:

    安装 python-slugify

    pip install python-slugify

    【讨论】:

      【解决方案2】:

      slugify package you installed 不是为 python 3 构建的,它目前只支持 python 2。而且它不太可能得到更新。最简单的判断方法之一是,在整个源代码中,它使用了 python 2 关键字 unicode,而它在 python 3 中不存在。

      你可能做到了:

      pip install slugify
      

      那是一个过时的包,不是你链接的那个。

      要安装您链接的 slugify 包,https://pypi.python.org/pypi/python-slugify,安装时它称为python-slugify,它支持所有最新的 python 版本。并且有更多的功能。

      pip install python-slugify
      

      并以与其他包相同的方式导入:

      from slugify import slugify
      

      注意:您必须删除您安装的原始包,因为它们使用相同的名称。

      【讨论】:

      • 请注意 - 删除原始包:pip uninstall slugify。它可能还需要重新启动环境(例如,我需要用 Hydrogen 重新启动 Atom)
      • 能否以某种方式从 python-slugify 更新 slugify 以避免再次出现这种混淆?
      猜你喜欢
      • 2014-02-18
      • 2019-06-15
      • 2013-06-19
      • 2021-05-20
      • 2019-10-22
      • 2014-12-02
      • 2014-04-24
      • 2013-04-17
      • 2016-08-01
      相关资源
      最近更新 更多