从Python中的Unicode字符串替换非ASCII字符 [英] Replace non-ascii chars from a unicode string in Python

查看:119
本文介绍了从Python中的Unicode字符串替换非ASCII字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I replace non-ascii chars from a unicode string in Python?

这是我为给定输入指定的输出:

This are the output I spect for the given inputs:

音乐->音乐

cartón->纸箱

caño-> cano

caño -> cano

Myaybe带有dict,其中á"是键,而"a"是值?

Myaybe with a dict where 'á' is a key and 'a' a value?

推荐答案

如果您要做的就是将重音字符降级为非重音等效字符:

If all you want to do is degrade accented characters to their non-accented equivalent:

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

这篇关于从Python中的Unicode字符串替换非ASCII字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆