我的输出看起来像'àéêöhello!'。我需要将输出更改为'aeeohello',只需将字符à替换为a即可。
我的输出看起来像'àéêöhello!'。我需要将输出更改为'aeeohello',只需将字符à替换为a即可。
import unicodedata
def strip_accents(text):
try:
text = unicode(text, 'utf-8')
except NameError: # unicode is a default on python 3
pass
text = unicodedata.normalize('NFD', text)\
.encode('ascii', 'ignore')\
.decode("utf-8")
return str(text)
s = strip_accents('àéêöhello')
print s
str(text)
? normalize
似乎会返回一个字符串(Python 3.9)。 - Safwan Samsudeenunicode
。Python 3.8。 意思是代码中使用了unicode
这个名称,但是在运行时却找不到该名称的定义。 - TomSawyerimport unidecode
somestring = "àéêöhello"
#convert plain text to utf-8
u = unicode(somestring, "utf-8")
#convert utf-8 to normal text
print unidecode.unidecode(u)
输出:
aeeohello
Alpesh Valaki的答案是最好的,但我需要做一些调整才能使其正常工作:
# I changed the import
from unidecode import unidecode
somestring = "àéêöhello"
#convert plain text to utf-8
# replaced unicode by unidecode
u = unidecode(somestring, "utf-8")
#convert utf-8 to normal text
print(unidecode(u))