from collections import OrderedDict
with open("file1.txt") as f1, open("file2.txt") as f2:
d = OrderedDict.fromkeys(map(str.rstrip, f2),"")
for line in f1:
if line.strip():
data,k = line.rsplit(None, 1)
if k in d:
d[k] = data
for k,v in d.items():
print(k,v)
输出:
^{pr2}$
如果每行实际上有两列,即:
c321 bd11
bc2d cf17
您需要拆分行以获取每个键:
from collections import OrderedDict
with open("file1.txt") as f1, open("file2.txt") as f2:
d = OrderedDict((k, "") for line in f2 for k in line.split())
for line in f1:
if line.strip():
data, k = line.rsplit(None, 1)
if k in d:
d[k] = data
如果是c321、bd11等。。不能重复只使用dict:
输出:
^{pr2}$如果每行实际上有两列,即:
您需要拆分行以获取每个键:
相关问题 更多 >
编程相关推荐