Weather Underground API to get historical data in Python

6.1k Views Asked by At

I am trying to pull historical data from the Weather Underground API. I adapted their Python example code (see below). When I run this I get an exception "TypeError: list indices must be integers, not str" The JSON stream includes a bunch of fields with daily summary information (Dailysummary), but I cannot get out and any of the values they have in this list.

I put the URL into a JSON viewer to look at the structure, and cannot figure out what I am doing wrong. Any help would be greatly 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()
3

There are 3 best solutions below

4
On

It says your problem right in the error: You can't index a list with a string:

>>> [1]['0']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not str
>>> [1][0]
1
>>>

But you do it here:

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

To fix your problem, make your indexes integers:

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

The way to access the contents in parsed_json dict is using the keys. You can find the keys you're interested in using print parsed_json['history']['dailysummary'].keys()

If you do that you'll see that one of the keys is u'snow' which is a unicode string.

You can use that string to access the data you're looking for so instead of:

parsed_json['history']['dailysummary']['0']

what you need is:

parsed_json['history']['dailysummary']['snow']

0
On

I had a similar problem. Since it is a list of length 1, you can access the list item by the index 0. Now, if you check the type of the value item in index 0, it is a dictionary.

type(parsed_json['history']['dailysummary'][0])
<type 'dict'>

Now, that you know it is a dictionary, you can use keys to access the values. In your case.

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

This give you the value you are looking for.

Let me know if this helps.