Skip to main content
 首页 » 编程设计

Python之如何改变像é这样的字母

2025年05月04日21mfryf

Python 中是否有一个函数可以将 é 转换为 e,或 í 转换为 i?

请您参考如下方法:

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

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?'