2 回答
TA贡献1946条经验 获得超3个赞
mapping = {'abc':'123', 'cde':'456'}
all_files = ['abc.txt','cde.txt']
out_files = ['abc_output.txt','cde_output.txt']
read_dict = {}
for in_f, out_f in zip(all_files, out_files):
#print (in_f, out_f)
with open(in_f,'r') as read_file:
lines = read_file.readlines()
with open(out_f,'w+') as write_file:
for line in lines:
for key in mapping:
line = line.replace(key, mapping[key])
#write_file.writelines(line)
TA贡献1840条经验 获得超5个赞
你也可以这样做
mapping = {'abc':'123', 'cde':'456'}
all_files = ['abc.txt','cde.txt']
out_files = ['abc_output.txt','cde_output.txt']
read_dict = {}
for in_f, out_f in zip(all_files, out_files):
with open(in_f,'r') as read_file:
lines = read_file.readlines()
with open(out_f,'w+') as write_file:
for line in lines:
for key in mapping:
line = line.replace(key, mapping[key])
#write_file.writelines(line)
添加回答
举报