皆様いつもお世話になっております。
このコードで出力すると
python
1import urllib.request 2import json 3 4url = 'http://weather.livedoor.com/forecast/webservice/json/v1?city=080020' 5html = urllib.request.urlopen(url) 6jsonfile = json.loads(html.read().decode('utf-8')) 7print(jsonfile['forecast']) 8 9 10 11結果: 12[{'dateLabel': '今日', 'telop': '晴時々曇', 'date': '2017-07-11', 'temperature': {'min': None, 'max': {'celsius': '33', 'fahrenheit': '91.4'}}, 'image': {'width': 50, 'url': 'http://weather.livedoor.com/img/icon/2.gif', 'title': '晴時々曇', 'height': 31}}, {'dateLabel': '明日', 'telop': '曇り', 'date': '2017-07-12', 'temperature': {'min': {'celsius': '24', 'fahrenheit': '75.2'}, 'max': {'celsius': '32', 'fahrenheit': '89.6'}}, 'image': {'width': 50, 'url': 'http://weather.livedoor.com/img/icon/8.gif', 'title': '曇り', 'height': 31}}, {'dateLabel': '明後日', 'telop': '曇のち雨', 'date': '2017-07-13', 'temperature': {'min': None, 'max': None}, 'image': {'width': 50, 'url': 'http://weather.livedoor.com/img/icon/13.gif', 'title': '曇のち雨', 'height': 31}}]
この中の「2017-07-11」の「晴時々曇」だけを出力したいのですが、
Python
1print(jsonfile['forecast']['telop'])
とすると、コンパイラに「Traceback (most recent call last):
File "tst.py", line 7, in <module>
print(jsonfile['forecasts']['telop'])
TypeError: list indices must be integers or slices, not str」
と怒られてしまいます。
どうしたら出力できるようになるのでしょうか。ご教授のほどよろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/07/11 03:32