Python - 如何更改像é这样的字母

时间:2017-05-21 20:54:27

标签: python string function diacritics

python中是否有一个函数将é转换为e或í转换为i?

1 个答案:

答案 0 :(得分:5)

是的,您可以通过以下过程过滤和映射字符串的字符:

from unicodedata import normalize, category

''.join([c for c in normalize('NFD',original) if category(c) != 'Mn'])

在此,我会根据您的问题过滤这些重音:

>>> original = 'Is there a function in python which converts letters like é to e, or í to i?'
>>> from unicodedata import normalize, category
>>> ''.join([c for c in normalize('NFD',original) if category(c) != 'Mn'])
'Is there a function in python which converts letters like e to e, or i to i?'