python根据一个文件中的key和另外一个文件key对比,同时提取value

整体思想是可以先获取每个文件的key,然后将相等的输出到list中,随后将第一个文件搞成dict,遍历1、2文件合成的list,然后和dict的key对比,如果相等则输出对应的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间的互相转换
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])

你可能感兴趣的:(Python和shell脚本,python,java,前端)