import re
from unicodedata import normalize
_punct_re = re.compile(r'[\t !"#$%&\'()*\-/<=>?@\[\\\]^_`{|},.]+')
def slugify(text, delim=u'-'):
"""Generates an slightly worse ASCII-only slug."""
result = []
for word in _punct_re.split(text.lower()):
word = normalize('NFKD', word).encode('ascii', 'ignore')
if word:
result.append(word)
return unicode(delim.join(result))
这种URL安全值的通用术语是"slug",生成一个值的过程称为“slugify”或“to slugify”。人们通常使用正则表达式来实现这一点;这里有一个(来源于this article on the subject),只使用stdlib模块:
链接的文章还有另外两个需要额外模块的选项。你知道吗
相关问题 更多 >
编程相关推荐