python中如何对多个字符快速替换

如题所述

python中快速进行多个字符替换的方法小结

先给出结论:

    要替换的字符数量不多时,可以直接链式replace()方法进行替换,效率非常高;

    如果要替换的字符数量较多,则推荐在 for 循环中调用 replace() 进行替换。

    可行的方法:

    1. 链式replace()

    ?

    1   string.replace().replace()   

    1.x 在for循环中调用replace() 「在要替换的字符较多时」

    2. 使用string.maketrans

    3. 先 re.compile 然后 re.sub

温馨提示:答案为网友推荐,仅供参考
相似回答