json - Iteritems a dict in python -


i've created python function iterating through multiple level dictionary, , execute second function ssocr needs 4 arguments: coord, background, foreground, type (they value of keys). dictionary taken json file.

document json

`

def parse_image(self, d):     bg = d['background']     fg = d['foreground']     results = {}     k, v in d['boxes'].iteritems():         if 'foreground' in d['boxes']:             myfg = d['boxes']['foreground']         else:             myfg = fg         if k != 'players_home' , k != 'players_opponent':             results[k] = myagonism.ssocr(v['coord'], bg, myfg, v['type'])      results['players_home'] = {}     k, v in d['boxes']['players_home'].iteritems():         if 'foreground' in d['boxes']['players_home']:             myfg = d['boxes']['players_home']['foreground']         else:             myfg = fg         if k != 'background' , 'foreground':             k2, v2 in d['boxes']['players_home'][k].iteritems():                 if k2 != 'fouls':                     results['players_home'][k] = myagonism.ssocr(v2['coord'], bg, myfg, v2['type'])     return results 

i have error in foreground check in second last iteritems, key number overrides key score

your problem here:

if k != 'background' , 'foreground':     # 

which isn't doing check think it's doing. you're trying

if (k != "background") , ('foreground'):     # 

which evaluates true (since non empty string considered "truthy").

just change line to:

if k not in ('background', 'foreground'):     # stuff 

or same way did further in function (if k != 'players_home' , k != 'players_opponent':) , should in business.


Comments