python 字符串替换

text = open('text.txt','r',encoding='utf-8').read()
old,new = ['(',')',',','.'],['(',')',',','。']
def replace(old,new):
data = ''
for i in text:
if i in old: #为什么这里要加个判断了,因为可以减少计算量,不用每个都遍历,符合条件才遍历。
for o,j in enumerate(old):
if i == j:
i = new[o]
data = '%s%s' % (data,i)
print(data)
replace(old,new)
posted @ 2022-06-20 16:23  记录——去繁就简  阅读(42)  评论(0)    收藏  举报