Python技巧-实现批量替换字符串
作者:互联网
场景一 · 批量将不同的字符串替换为不同内容
# 要替换的内容 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))
标签:dl,批量,Python,repl,replist,字符串,txt,替换 来源: https://www.cnblogs.com/ymer/p/16692385.html