Wed
Nov
18
Creating unicode permalinks in Python (even from Greek!)
Optimizing this blog post, which shows you how to create a greek to greeklish “translator”, I’ve created a permalink function that creates urls from greek titles (but you can use your language).
Enjoy!
def create_permalink(s):
“”“
Create greeklish permalinks.
Also works for english.
“”“
import string
import re
input_string = s.lower().decode(‘utf-8’).encode(‘iso-8859-7’, ‘replace’)
from_chars = ‘αβγδεζηθικλμνξοπρσςτυφχψωάήέίόύώϊ’.decode(‘utf-8’).encode(‘iso-8859-7’, ‘replace’)
to_chars = ‘abgdezh8iklmn3oprsstufxywaheiouwi’
translation_table = string.maketrans(from_chars, to_chars)
input_string = string.translate(input_string, translation_table)
return re.compile(“\W+”, re.UNICODE).sub(“_”, input_string)