python replace accented characters code 1

python replace accented characters code

import unicodedata

def strip_accents(text):

        text = unicode(text, 'utf-8')
    except NameError: # unicode is a default on python 3 

    text = unicodedata.normalize('NFD', text)\
           .encode('ascii', 'ignore')\

    return str(text)

s = strip_accents('àéêöhello')

print s

Here is what the above code is Doing:
1. Convert the string to unicode.
2. Normalize the unicode string using the unicodedata module.
3. Encode the normalized unicode string using the ascii codec.
4. Decode the encoded string using the utf-8 codec.
5. Return the decoded string.

Similar Posts