Python: replace french letters with english

David Ferris picture David Ferris · Dec 6, 2016 · Viewed 7.4k times · Source

Would like to replace all the french letters within words with their ASCII equivalent.

letters = [['é', 'à'], ['è', 'ù'], ['â', 'ê'], ['î', 'ô'], ['û', 'ç']]

for x in letters:
   for a in x:
        a = a.replace('é', 'e')
        a = a.replace('à', 'a')
        a = a.replace('è', 'e')
        a = a.replace('ù', 'u')
        a = a.replace('â', 'a')
        a = a.replace('ê', 'e')
        a = a.replace('î', 'i')
        a = a.replace('ô', 'o')
        a = a.replace('û', 'u')
        a = a.replace('ç', 'c')

print letters[0][0]

This code prints é however. How can I make this work?

Answer

logan rakai picture logan rakai · Dec 6, 2016

May I suggest you consider using translation tables.

translationTable = str.maketrans("éàèùâêîôûç", "eaeuaeiouc")

test = "Héllô Càèùverâêt Jîôûç"
test = test.translate(translationTable)
print(test)

will print Hello Caeuveraet Jiouc. Pardon my French.