-4

i got a problem trying to encore non ASCII characters. I have this function :

#function to treat special characters 
tagsA=["À","Á","Â","à","á","â","Æ","æ"]
tagsC=["Ç","ç"]
tagsE=["È","É","Ê","Ë","è","é","ê","ë"]
tagsI=["Ì","Í","Î","Ï","ì","í","î","ï"]
tagsN=["Ñ","ñ"]
tagsO=["Ò","Ó","Ô","Œ","ò","ó","ô","œ"]
tagsU=["Ù","Ú","Û","Ü","ù","ú","û","ü"]
tagsY=["Ý","Ÿ","ý","ÿ"]

def toASCII(word):
    for i in range (0, len(word),1):
        if any(word[i] in s for s in tagsA):
           word[i]="a"
        if any(word[i] in s for s in tagsC):
           word[i]="c"
        if any(word[i] in s for s in tagsE):
           word[i]="e"
        if any(word[i] in s for s in tagsI):
           word[i]="i"
        if any(word[i] in s for s in tagsN):
           word[i]="n"
        if any(word[i] in s for s in tagsO):
           word[i]="o"
        if any(word[i] in s for s in tagsU):
           word[i]="u"
        if any(word[i] in s for s in tagsY):
           word[i]="y"
    print word
    return word

i get this error usually : UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1: ordinal not in range(128)

tried to change encoding to utf8 but it doesn't change the issue.

 # -*- coding: utf-8 -*-
Samuel
  • 23
  • 9

1 Answers1

2

You can use the unicodedata module to remove all the accents from string.

Ex:

import unicodedata
print unicodedata.normalize('NFKD', u"ÀÁ").encode('ASCII', 'ignore')

Output:

AA
Rakesh
  • 81,458
  • 17
  • 76
  • 113