Reputation: 8554
I am trying to compare values of key "rrname" in a json format file ,jadata dic (Each line is a dictionary) with keys of dictionary d which is already loaded in memory.
Here is my code: import simplejson
ap = '/data/data/2014/A.1/ap.txt'
ddb = '/data/data/2014/A.1/test'
d={}
f = open(ap,'r')
g = open(ddb,'r')
for line in f:
domain,bl_date= line.split('|')
d[domain]=bl_date
for line in g:
line=line.strip('')
try:
jdata = simplejson.loads(line)
if jdata.get('rrname') == d.keys():
print rrname
except:
raise
here is my ddb file :
{"rrname": "bba186684.alshamil.net.ae.", "time_last": 1389295255, "time_first": 1389241418, }
{"rrname": "bba186686.alshamil.net.ae.", "time_last": 1390910891, "time_first": 1390910891}
{"rrname": "0001ewm.rcomhost.com", "time_last": 1390147425, "time_first": 1390124988}
here is ap file:
0001elk.rcomhost.com|1391726703
0001ewm.rcomhost.com|1393472522
0001qz6.wcomhost.com|1399977648
when I run this code, it cannot find the similarities, although there is. Can somebody help me with this?
Upvotes: 0
Views: 140
Reputation: 881705
jdata.get('rrname') == d.keys()
will always fail -- the single entry on the left of the ==
won't equal all entries on the right as you're asking.
Rather, check whether:
jdata.get('rrname') in d
The in
operator looks for the left side to be contained in the right side. It's important for performance to use d
, not d.keys()
, as the right side, since checking for containment in a dictionary is much faster than checking in a list (as .keys
would be in Python 2, which I guess is what you're using, even though you don't tell us!, based on the syntax for that print
:-).
Upvotes: 2