pythonstringasciifrench

Python: replace french letters with english


I 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?


Solution

  • 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.