Python 中的字符串slugification
我正在寻找“slugify”字符串的最佳方法什么是“slug”,我当前的解决方案基于 这个食谱
我已经将其稍微更改为:
s = 'String to slugify'
slug = unicodedata.normalize('NFKD', s)
slug = slug.encode('ascii', 'ignore').lower()
slug = re.sub(r'[^a-z0-9]+', '-', slug).strip('-')
slug = re.sub(r'[-]+', '-', slug)
有人看到这段代码有任何问题吗?它工作正常,但也许我错过了一些东西或者你知道更好的方法?
I am in search of the best way to "slugify" string what "slug" is, and my current solution is based on this recipe
I have changed it a little bit to:
s = 'String to slugify'
slug = unicodedata.normalize('NFKD', s)
slug = slug.encode('ascii', 'ignore').lower()
slug = re.sub(r'[^a-z0-9]+', '-', slug).strip('-')
slug = re.sub(r'[-]+', '-', slug)
Anyone see any problems with this code? It is working fine, but maybe I am missing something or you know a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(12)
有一个名为 awesome-slugify 的 python 包:
pip install awesome-slugify
工作原理如下:
from slugify import slugify
slugify('one kožušček') # one-kozuscek
awesome-slugify: com/dimka665/awesome-slugify">awesome-slugify github 页面
def slugify(value):
"""
Converts to lowercase, removes non-word characters (alphanumerics and
underscores) and converts spaces to hyphens. Also strips leading and
trailing whitespace.
"""
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
value = re.sub('[^\w\s-]', '', value).strip().lower()
return mark_safe(re.sub('[-\s]+', '-', value))
slugify = allow_lazy(slugify, six.text_type)
这是 django.utils.text 中存在的 slugify 函数
这应该可以满足您的要求。
问题在于 ascii 标准化行:
slug = unicodedata.normalize('NFKD', s)
它被称为 unicode 标准化,它不会分解大量字符到 ascii。例如,它会从以下字符串中去除非 ascii 字符:
Mørdag -> mrdag
Æther -> ther
更好的方法是使用 unidecode 模块尝试将字符串音译为 ascii。因此,如果将上面的行替换为:
import unidecode
slug = unidecode.unidecode(s)
对于上面的字符串以及许多希腊语和俄语字符,您也会获得更好的结果:
Mørdag -> mordag
Æther -> aether
GitHub 上的几个选项:
- https://github.com/dimka665/awesome-slugify
- < a href="https://github.com/un33k/python-slugify" rel="nofollow">https://github.com/un33k/python-slugify
- https://github.com/mozilla/unicode-slugify
每个 API 支持的参数略有不同,因此您需要仔细查看弄清楚你更喜欢什么。
特别要注意它们为处理非 ASCII 字符提供的不同选项。 Pydanny 写了一篇非常有用的博客文章,说明了这些 slugify'ing 库中的一些 unicode 处理差异: any-string.html" rel="nofollow">http://www.pydanny.com/awesome-slugify- human-read-url-slugs-from-any-string.html 这篇博文稍微已经过时了,因为 Mozilla 的 unicode-slugify
不再是 Django 特定的。
另请注意,目前 awesome-slugify
是 GPLv3,尽管存在一个未解决的问题,作者表示他们更愿意以 MIT/BSD 形式发布,只是不确定合法性:https://github.com/dimka665/awesome-slugify/issues/24
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
有一个名为
python-slugify
的 python 包,它做了一个漂亮的slugify 做得很好:工作原理如下:
请参阅 更多示例
这个包的功能比您要多一些发布(看看源代码,它只是一个文件)。该项目仍然活跃(在我最初回答的前 2 天更新,九年后(最后一次检查 2022-03-30),它仍然更新)。
小心:还有第二个包,名为
slugify
。如果您同时拥有它们,则可能会遇到问题,因为它们具有相同的导入名称。刚刚名为slugify
的那个并没有完成我快速检查的所有操作:“Ich heiße”
变成了“ich-heie”
(应该是 < code>"ich-heisse"),因此在使用pip
或easy_install
时,请务必选择正确的。There is a python package named
python-slugify
, which does a pretty good job of slugifying:Works like this:
See More examples
This package does a bit more than what you posted (take a look at the source, it's just one file). The project is still active (got updated 2 days before I originally answered, over nine years later (last checked 2022-03-30), it still gets updated).
careful: There is a second package around, named
slugify
. If you have both of them, you might get a problem, as they have the same name for import. The one just namedslugify
didn't do all I quick-checked:"Ich heiße"
became"ich-heie"
(should be"ich-heisse"
), so be sure to pick the right one, when usingpip
oreasy_install
.