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)
添加回答
舉報