chartr()
(R) 函数让生活变得如此简单:
txtdata = "my têxt is plaîn of accent"
chartr("îêéè", "ieee", txtdata)
return "my text is plan of accent"
在 Python 中,re.sub()
函数只对第二个参数取一个替换值:
re.sub("[éè]", "e", txtdata)
是否有与 chartr() 等效的 Python 函数?
最佳答案
我相信 str.translate
更适合这样的重音翻译任务。
out = "my têxt is plaîn of accent".translate(str.maketrans("îêéè", "ieee"))
print(out)
'my text is plain of accent'
100000 loops, best of 3: 3.05 µs per loop
关于Python 相当于 chartR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45976679/