Wunderground API to get hourly forecast returns error. - Python

送分小仙女□ 提交于 2019-12-14 03:11:21

问题


I'm trying to get the hourly forecast from the Wunderground API but my code returns this error.

Traceback (most recent call last): File "weathergraph.py", line 10, in forecast = parsed_json['hourly_forecast']['FCTTIME']['temp']['english'] TypeError: list indices must be integers, not str

This is my code.

f=urllib2.urlopen('http://api.wunderground.com/api/mykey/hourly/q/NY/New_York_City.json')

json_string = f.read()

parsed_json = json.loads(json_string)

forecast = parsed_json['hourly_forecast']['FCTTIME']['temp']['english']

f.close()

parsed_json = http://pastie.org/3905346


回答1:


1) The value of hourly_forecast is a list of dicts, not a dict. Looks like about 36 in the list.

2) temp is not an element of FCTTIME. They are at the same level

This should not generate an error:

forecast = parsed_json['hourly_forecast'][-1]['temp']['english'] 

It looks like the list is in order by time, so the last one is most recent. Checking the contents of FCTTIME will tell you whether it is different from the last time that you read it.



来源:https://stackoverflow.com/questions/10572150/wunderground-api-to-get-hourly-forecast-returns-error-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!