ASCII非ASCII转换Python 2.7
我在尝试重新编码非ASCII字符时遇到问题。 我有这个功能:ASCII非ASCII转换Python 2.7,python,ascii,non-ascii-characters,Python,Ascii,Non Ascii Characters,我在尝试重新编码非ASCII字符时遇到问题。 我有这个功能: #function to treat special characters tagsA=["À","Á","Â","à","á","â","Æ","æ"] tagsC=["Ç","ç"] tagsE=["È","É","Ê","Ë","è","é","ê","ë"] tagsI=["Ì","Í","Î","Ï","ì","í","î","ï"] tagsN=["Ñ","ñ"] tagsO=["Ò","Ó","Ô","Œ","ò","
#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模块删除字符串中的所有重音符号
例:
输出:
使用步长为1的范围?为什么指定1?@ArnavBorborah我对整个字符串逐个字符进行处理,您可以将步骤留空,它将自动设置为1。这是一种非常糟糕的方法。您将需要处理许多边缘情况。Python支持这一内置功能。为什么不使用这些函数?@OptimusCrime对于这样的问题,我可以使用哪些函数?UnicodeDecodeError并不是唯一的问题。
import unicodedata
print unicodedata.normalize('NFKD', u"ÀÁ").encode('ASCII', 'ignore')
AA