用Python中的unicode字符串替换非ASCII字符

问题描述:

如何从Python中的unicode字符串替换非ASCII字符?用Python中的unicode字符串替换非ASCII字符

这是我SPECT对于给定的输入的输出:

曲艺 - > MUSICA

纸箱 - >纸箱

卡诺 - >卡诺

Myaybe用一个字典其中“a '是一个关键和'一个'的价值?

+2

可能重复[什么是去除一个Python unicode字符串口音的最好方法?(http://*.com/questions/517923/what-is-the-best-way-to-除去-口音-IN-A-蟒-Unicode的字符串) – nosklo 2010-09-13 22:04:15

如果你想要做的就是降低重音符号,以他们的非重音等效:

>>> import unicodedata 
>>> unicodedata.normalize('NFKD', u"m\u00fasica").encode('ascii', 'ignore') 
'musica' 

现在,要补充这个问题的答案: 这可能是你的数据不统一来的情况下(即你正在阅读一个文件与另一种编码,你不能在字符串前添加“u”)。这里有一个可能起作用的片段(主要是那些用英文阅读文件的人)。的

import unicodedata 
unicodedata.normalize('NFKD',unicode(someString,"ISO-8859-1")).encode("ascii","ignore")