[Tutor] getting and using information dict objects

Mitya Sirenef msirenef at lightbird.net
Sun Feb 24 03:55:58 CET 2013

On 02/23/2013 09:40 PM, Matthew Johnson wrote:
> For the sake of those who finds  this thread -- the date / value pairs
 > can be printed by the following:
 > import fred
 > fred.key(fredKey)
 > gnpObvs = fred.observations('GNPCA')
 > for i in range(1, len(gnpObvs['observations']['observation'])):
 > print gnpObvs['observations']['observation'][i]['date'],
 > gnpObvs['observations']['observation'][i]['value']
 > mj

You can do this in a simpler way (in python 2.x):

observation = gnpObvs["observation"]["observation"]

for obsdict in observation.values()[1:]:
     print obsdict["date"]

if you need the 'i' counter, you can do:

for i, obsdict in enumerate(observation.values())[1:]:
     print obsdict["date"]

HTH, -m

Lark's Tongue Guide to Python: http://lightbird.net/larks/

Do whatever you will, but first be such as are able to will.
Friedrich Nietzsche

More information about the Tutor mailing list