Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python generic function to replace special characters

I have been looking for a while now but I am not able to find a proper solution.

I have a database with Dutch, French and German words which all have their special characters. e.g. é, è, ß, ç, etc...
For some cases, like in a url, I would like to replace these with alphanumeric characters. respectively e, e, ss, c, etc...

Is there a generic function or Python package that does this?

I could do this with Regex of course, but something generic would be great here.

Thanks.

like image 745
Johan Vergeer Avatar asked Sep 10 '26 05:09

Johan Vergeer


2 Answers

try this package: https://pypi.python.org/pypi/Unidecode

>>> import unidecode
>>> unidecode.unidecode(u'çß')
'css'
like image 146
Vladimir Lemberg Avatar answered Sep 11 '26 19:09

Vladimir Lemberg


As you say, this could be done using a Regex sub. You would of course need to include upper and lowercase variants.

import re

data = "é, è, ß, ç, äÄ"
lookup = {'é':'e', 'è':'e', 'ß':'ss', 'ç':'c', 'ä':'a', 'Ä':'A'}
print re.sub(r'([éèßçäÄ])', lambda x: lookup[x.group(1)], data)

This would display the following:

e, e, ss, c, aA
like image 28
Martin Evans Avatar answered Sep 11 '26 17:09

Martin Evans



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!