ASCII non ASCII translation Python 2.7



我遇到了一个问题,试图包含非ASCII字符。我有这个功能:

#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

我通常会出现此错误: UNICODEDECODEERROR:" ASCII"编解码器无法在位置1中解码字节0xc3:序列不在范围(128(

试图将编码更改为UTF8,但不会更改问题。

 # -*- coding: utf-8 -*-

您可以使用 unicodedata 模块从字符串中删除所有口音。

ex:

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

输出

AA

相关内容

  • 没有找到相关文章

最新更新