我有几个文本文件,其中包含带有变音符号的字符,例如
è
, á
, ô
等等。我想用 e
, a
, o
等等替换这些字符。请问怎样在Python中实现呢?谢谢帮助!è
, á
, ô
等等。我想用 e
, a
, o
等等替换这些字符。请问怎样在Python中实现呢?谢谢帮助!尝试使用unidecode(您可能需要安装它)。
>>> from unidecode import unidecode
>>> s = u"é"
>>> unidecode(s)
'e'
以下是您可以做的示例:
accented_string = u'Málaga'
`enter code here`# accented_string is of type 'unicode'
import unidecode
unaccented_string = unidecode.unidecode(accented_string)
# unaccented_string contains 'Malaga'and is of type 'str'
您的问题有一个非常相似的例子,请查看: 如何在Python Unicode字符串中最好地去除重音符号?
unidecode
包。它适用于小写和大写字母。pip3
而不是pip
)$ pip install unidecode
from unidecode import unidecode
text = ["ÉPÍU", "Naïve Café", "EL NIÑO"]
text1 = [unidecode(s) for s in text]
print(text1)
# ['EPIU', 'Naive Cafe', 'EL NINO']
text2 = [unidecode(s.lower()) for s in text]
print(text2)
# ['epiu', 'naive cafe', 'el nino']
text.replace('é','e')
- user955340