Extract nested dictionary values in Python -


i think i'm close spinning in circles on one. have dictionary in python has been imported json. raw data post here. code works want , prints want, except returns last value dictionary. how modify return values , print how shown here? want take data , add database 1 entry.

code:

text = json.loads(content)  = {}  def extract(dictin, dictout):     key, value in dictin.iteritems():         if isinstance(value, dict):              extract(value, dictout)         elif isinstance(value, list):             in value:                 extract(i, dictout)         else:             dictout[key] = value  extract(text, a)  k, v in a.iteritems():     print k, ":", v 

result should following, have other 40 or entries. code shows last entry:

datetime : 2014-06-10t20:00:00-0600 date : 2014-06-10 href : http://xxxxxxxxxxxx.json lng : -94.5554 id : 17551289 createdat : 2013-07-30t12:18:56+0100 city : kansas city, mo, billing : headline totalentries : 37 type : concert billingindex : 1 status : ok perpage : 50 setlistshref : xxxxxxxxxxxx:51b7f46f-6c0f-46f2-9496-08c9ec2624d4/setlists.json lat : 39.0763 displayname : ben folds eventshref : http://xxxxxxx.08c9ec2624d4/calendar.json popularity : 0.0 uri : xxxxxxxxxxxxxxxxxx mbid : xxxxxxxxxxx ontouruntil : 2014-06-10 time : 20:00:00 page : 1 

the problem dictout[key] = value

in python dictionary keys unique

assume

_d = {1: 'one', 2: 'two'}  >>> print _d {1: 'one', 2: 'two'}  >>> _d[1] = 'another one' >>> print _d {1: 'another one', 2: 'two'} 

i guess in loop overwriting value of existing key, that's why last entry getting stored !.

try changing data structure, list of dictionaries, output may like,

my_out_list = [{1: 'one', 2: 'two'}, {1: 'another one', 2: 'two'}] 

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -