Ganesh Basuvaraj
Ganesh Basuvaraj

Reputation: 271

How to replace accented characters?

My output looks like 'àéêöhello!'. I need change my output like this 'aeeohello', Just replacing the character à as a like this.

Upvotes: 27

Views: 50282

Answers (3)

Samuel Lacombe
Samuel Lacombe

Reputation: 91

Alpesh Valaki's answer is the "nicest", but I had to do some adjustments for it to work:

# I changed the import
from unidecode import unidecode

somestring = "àéêöhello"

#convert plain text to utf-8
# replaced unicode by unidecode
u = unidecode(somestring, "utf-8")

#convert utf-8 to normal text
print(unidecode(u))

Upvotes: 6

Alpesh Valaki
Alpesh Valaki

Reputation: 1631

import unidecode

somestring = "àéêöhello"

#convert plain text to utf-8
u = unicode(somestring, "utf-8")
#convert utf-8 to normal text
print unidecode.unidecode(u)

Output:

aeeohello

Upvotes: 13

Eswara Moorthy
Eswara Moorthy

Reputation: 529

Please Use the below code:

import unicodedata

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

    text = unicodedata.normalize('NFD', text)\
           .encode('ascii', 'ignore')\
           .decode("utf-8")

    return str(text)

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

print s

Upvotes: 52

Related Questions