The overall idea is to get each file first key, Then output the same to list in , Then the first file is made into dict, Traverse 1、2 File composed list, And then dict Of key contrast , If they are equal, the corresponding value
#!/usr/bin/python
import json
file = '/Users/jian.wang13/code/test/wenjian1.txt'
file2 = '/Users/jian.wang13/code/test/wenjian2.txt'
wenjian1_list = []
with open(file,'r') as f:
for i in f:
tmp = i.split(' ')
# print(type(tmp))
# print(tmp[0])
# print(type(tmp))
wenjian1_list.append(tmp[0])
print('###################################')
wenjian2_list = []
with open(file2,'r') as f2:
for j in f2:
tmp = j.strip()
wenjian2_list.append(tmp)
xiangdeng_list = []
for i in wenjian1_list:
for j in wenjian2_list:
if i == j:
xiangdeng_list.append(i)
# print(i)
print('@@@@@@@@@@@@@@@@@@@@@@@@@@')
#str、lsit、dict Mutual conversion between
wenjian3_list = []
with open(file,'r') as f3:
for i in f3:
tmp = i.split(' ')
# d_tmp = json.loads(tmp)
wenjian3_list.append(tmp[1])
#print(wenjian3_list)
d = zip(wenjian1_list,wenjian3_list)
#print(dict(d))
dict1 = dict(d)
for i in xiangdeng_list:
for j in dict1.keys():
if i == j:
print(dict1[j])