程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python compares the key in one file with the key in another file, and extracts value at the same time

編輯:Python
 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])

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved