Skip to main content
 首页 » 编程设计

Python 相当于 chartR

2025年05月04日14softidea

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