You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1.2 KiB
1.2 KiB
Slugify in Python 3
- date
2016-12-08T12:54:19Z
- category
blog
- tags
development,python
- url
2016/12/08/slugify-in-python3/
- save_as
2016/12/08/slugify-in-python3/index.html
- status
published
- author
Gergely Polonkai
Today I needed a function to create a slug (an ASCII-only representation of a string). I went Googling a bit, and found an excellend Flask snippet. Problem is, it is designed for Python 2, so I came up with a Python 3 version.
import re
from unicodedata import normalize
= re.compile(r'[\t !"#$%&\'()*\-/<=>?@\[\\\]^_`{|},.]+')
_punctuation_re
def slugify(text, delim='-'):
"""
Generate an ASCII-only slug.
"""
= []
result for word in _punctuation_re.split(text.lower()):
= normalize('NFKD', word) \
word 'ascii', 'ignore') \
.encode('utf-8')
.decode(
if word:
result.append(word)
return delim.join(result)
As I don’t really like the transliteration done in the first example (e.g. converting ü to ue), I went with the second example.