python中快速进行多个字符替换的方法小结
先给出结论: 要替换的字符数量不多时,可以直接链式replace()方法进行替换,效率非常高; 如果要替换的字符数量较多,则推荐在 for 循环中调用 replace() 进行替换。 可行的方法: 1. 链式replace() string.replace().replace() 1.x 在for循环中调用replace() 「在要替换的字符较多时」 2. 使用string.maketrans 3. 先 re.compile 然后 re.sub ...... def a(text): chars = "" for c in chars: text = text.rep
下载地址
用户评论