Python---两个文件相互对应
解决问题:
需把A 文件内容 和 B文件内容 一一对应后写入C文件中,如:1:a/n
A.txt文件:1/n 2/n 3/n 4/n 5/n 6/n 7/n 8/n 9/n 10
B.txt文件 :a/n b/n c/n d/n e/n f/n g/n h/n i/n j/n k/n l/n m
d = {} with open('a.txt')as f1,open('b.txt')as f2: f11,f22 = f1.readlines(),f2.readlines() data = dict(zip(f11,f22)) keys = data.keys() #取到所有的key for i in keys: k = i.strip() y = data.get(i) #取到value d[k]=y for l,j in d.items(): msg = "%s: %s"%(l,j) with open('c.txt','a+') as fw: fw.writelines(msg)
本文来自博客园,作者:他还在坚持嘛,转载请注明原文链接:他还在坚持嘛 https://www.cnblogs.com/brf-test/p/14243533.html