The SSH host key has changed on 8 April, 2022 to this one: SHA256:573uTBSeh74kvOo0HJXi5ijdzRm8me27suzNEDlGyrQ
Sources of the site gergely.polonkai.eu, Jekyll version
You can not 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

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

_punctuation_re = re.compile(r'[\t !"#$%&\'()*\-/<=>?@\[\\\]^_`{|},.]+')

def slugify(text, delim='-'):
    """
    Generate an ASCII-only slug.
    """

    result = []
    for word in _punctuation_re.split(text.lower()):
        word = normalize('NFKD', word) \
               .encode('ascii', 'ignore') \
               .decode('utf-8')

        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.