比较匹配的两个词典

时间:2015-05-22 18:10:52

标签: python csv dictionary

不确定为什么这个for循环只打印字典的我想在两个词典之间进行比较,并在两个词典中打印匹配的键。匹配应该通过键完成。在这种情况下,密钥是CAS编号,例如71751412, Abamectin

# create the dictionaries
with open(r'C:\CAS-S.csv') as f:
d = dict(filter(None, csv.reader(f)))
print(d)

with open(r'C:\CAS-B.csv') as f:
g = dict(filter(None, csv.reader(f)))
print(g)

#match keys
for key in d:
  if key in g:
     print (d[key])

1 个答案:

答案 0 :(得分:2)

最简单的解决方案:

# match keys
for key in d:
  if key in g:
     print ('{}: {}'.format(key, d[key]))