unicode - Eliminate accents in python -


i have function remove accents in word

def remove_accents(word):     return ''.join(x x in unicodedata.normalize('nfkd', word) if x in string.ascii_letters) 

but when run it shows error

unicodedecodeerror: 'ascii' codec can't decode byte 0xf3 in position 3: ordinal not in range(128) 

the character in position 3 : ó

if input unicode string, works:

>>> remove_accents(u"foóbar") u'foobar' 

if isn't, doesn't. don't error describe, typeerror instead, , unicodedecodeerror if try cast unicode doing

>>> remove_accents(unicode("foóbar")) traceback (most recent call last):   file "<stdin>", line 1, in <module> unicodedecodeerror: 'ascii' codec can't decode byte 0xc3 in position 2: ordinal not in range(128) 

if problem, i.e. have python 2 str objects input, can solve decoding utf-8 first:

>>> remove_accents("foóbar".decode("utf-8")) u'foobar'