3

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()
Christophe Roussy
  • 16,299
  • 4
  • 85
  • 85
user2719147
  • 51
  • 2
  • 3
  • 1
    Possible duplicate of [Wunderground API to get hourly forecast returns error. - Python](https://stackoverflow.com/questions/10572150/wunderground-api-to-get-hourly-forecast-returns-error-python) – iled Feb 22 '18 at 23:52

3 Answers3

2

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]
  • Thank you for the quick response. Apologies as I am new to this. All that is being returned is a list with all the variable keys and their values. So this is a long list, but I want to pull specific values out for fog, maxdailytemp, etc. – user2719147 Aug 26 '13 at 19:06
  • @user2719147 - Then that isn't a list, but a dictionary. And, to access a value in a dictionary, use it's corresponding key like so: `mydict[key]` –  Aug 26 '13 at 19:11
  • well, when i check the type I am getting it back as a list with only 1 value. This is the whole problem otherwise I would grab the value without a problem. I don't know where things went wrong. – user2719147 Aug 26 '13 at 19:15
  • Maybe I should propose the question in another way. How can I get the daily summary values of fog, rain, snow, hail, thunder, etc from teh Weather Underground API? – user2719147 Aug 26 '13 at 19:23
1

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.

0

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']

Jamie Bull
  • 12,889
  • 15
  • 77
  • 116