场景一 · 批量将不同的字符串替换为不同内容
# 要替换的内容 key-value
replist = {"1":"一","2":"二","3":"三"}
txt = "111-222-333-112233-123"
def dl(t: str, repl: dict):
for i in repl:
t = t.replace(i,repl[i])
return txt
print(dl(txt,replist))
场景二 · 批量将不同的字符串替换为指定内容
# 要替换的内容,如将以下字符批量替换为 +
replist = ['1','2','3']
# 替换成什么字符
rt = "+"
# 测试文本
txt = '1a2b3d'
def dl(t: str, repl: list):
for i in repl:
t = t.replace(i,rt)
return t
print(dl(txt,replist))
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/289359.html