json - Weather Underground API to get historical data in Python -


i trying pull historical data weather underground api. adapted python example code (see below). when run exception "typeerror: list indices must integers, not str" json stream includes bunch of fields daily summary information (dailysummary), cannot out , of values have in list.

i put url json viewer @ structure, , cannot figure out doing wrong. appreciated.

import urllib2 import json f = urllib2.urlopen('http://api.wunderground.com/api/d08c4738fb303c66/geolookup/conditions/q/ca/san_francisco.json')  json_string = f.read() parsed_json = json.loads(json_string) location = parsed_json['location']['city'] temp_f = parsed_json['current_observation']['temp_f'] print "current temperature in %s is: %s" % (location, temp_f) f.close()  h = urllib2.urlopen('http://api.wunderground.com/api/d08c4738fb303c66/history_19760508/q/ca/san_francisco.json') json_string = h.read() parsed_json = json.loads(json_string) date = parsed_json['history']['utcdate']['pretty'] print date  print type(parsed_json['history'])  snow = parsed_json['history']['dailysummary']['0'] print snow h.close() 

it says problem right in error: can't index list string:

>>> [1]['0'] traceback (most recent call last):   file "<stdin>", line 1, in <module> typeerror: list indices must integers, not str >>> [1][0] 1 >>> 

but here:

snow = parsed_json['history']['dailysummary']['0'] 

to fix problem, make indexes integers:

snow = parsed_json['history']['dailysummary'][0] 

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -