JavaCake JavaCake - 5 months ago 10
Python Question

How to slugify while still maintaining UTF-8 letters

I am trying to slugify UTF-8 text, which in this case involves characters such as æøå, which I want to maintain.

When I use slugify it does not maintain the UTF-8 chars:

>>> from slugify import slugify
>>> slugify(u'æsel (øen)')
'aesel-oen'


It should be
æsel-øen
.

Answer

Use a different library to slugify; the unicode-slugify library output fits your requirements exactly:

$ bin/pip install unicode-slugify
Downloading/unpacking unicode-slugify
  Downloading unicode-slugify-0.1.1.tar.gz
  Running setup.py (path:/.../build/unicode-slugify/setup.py) egg_info for package unicode-slugify

Downloading/unpacking django (from unicode-slugify)
  Downloading Django-1.7-py2.py3-none-any.whl (7.4MB): 7.4MB downloaded
Installing collected packages: unicode-slugify, django
  Running setup.py install for unicode-slugify

Successfully installed unicode-slugify django
Cleaning up...
$ bin/python
Python 2.7.8 (default, Sep 19 2014, 22:15:41) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.51)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from slugify import slugify
>>> slugify(u'æsel (øen)')
u'\xe6sel-\xf8en'
>>> print slugify(u'æsel (øen)')
æsel-øen