私は天気予報のためのスクリプトを書いています。任意の助けをいただければ幸いですpython json TypeError:文字列インデックスは整数でなければなりません
File "./get_weather.py", line 79, in get_image
image_id = data1['weather'][0]['icon']
TypeError: string indices must be integers
:私は、関数を実行すると、私はエラーを次取得
def get_image(json_file):
json_file = "{}/{}".format(jsons_save_path, json_file)
f = open(json_file, 'r')
echo2 = f.read()
data1 = json.loads(echo2)
f.close()
print(data1)
print(type(data1))
image_id = data1['weather'][0]['icon']
print(image_id)
return
:
"{\"coord\":{\"lon\":21.01,\"lat\":52.23},\"weather\":[{\"id\":801,\"main\":\"Clouds\",\"description\":\"few clouds\",\"icon\":\"02d\"}],\"base\":\"stations\",\"main\":{\"temp\":21,\"pressure\":1023,\"humidity\":43,\"temp_min\":21,\"temp_max\":21},\"visibility\":10000,\"wind\":{\"speed\":2.6,\"deg\":20},\"clouds\":{\"all\":20},\"dt\":1502098200,\"sys\":{\"type\":1,\"id\":5374,\"message\":0.002,\"country\":\"PL\",\"sunrise\":1502075224,\"sunset\":1502129710},\"id\":756135,\"name\":\"Warsaw\",\"cod\":200}"
私のコード:私は、ファイルに保存されているJSONを次のようしています。
ようになり、コメントに頼まれた、ここに私の完全なコードです:私は取得しています
#!/usr/bin/env python3
import json
import http.client
import os
from shutil import copyfile
key = '...'
city_id = '756135'
conn = http.client.HTTPConnection('api.openweathermap.org')
payload = "{}"
jsons_save_path = '/shares/scripts/weather/jsons'
def get_current_weather():
conn.request("GET", "/data/2.5/weather?id=%s&units=metric&APPID=%s" % (city_id, key), payload)
res = conn.getresponse()
data = res.read()
data = data.decode("utf-8")
# print(json.loads(data))
save_to_file(data, 'current.json')
get_image('current.json')
return
def get_5_days():
conn.request("GET", "/data/2.5/forecast?id=%s&units=metric&APPID=%s" % (city_id, key), payload)
res = conn.getresponse()
data = res.read()
data = data.decode("utf-8")
# print(json.loads(data))
save_to_file(data, 'forecast.json')
return
def save_to_file(data, file_name):
tmp_file_name = "{}/{}.tmp".format(jsons_save_path, file_name)
final_file = "{}/{}".format(jsons_save_path, file_name)
with open(tmp_file_name, 'w') as outfile:
json.dump(data, outfile, ensure_ascii=False, separators=(',', ':'))
print("json files saved to tmp")
g = open(tmp_file_name, 'r')
echo = g.read()
g.close()
try:
test_json = json.loads(echo)
print('json is fine')
copyfile(tmp_file_name, final_file)
except ValueError as e:
print('invalid json with error: %' % e)
return None
return
def get_image(json_file):
json_file = "{}/{}".format(jsons_save_path, json_file)
f = open(json_file, 'r')
echo2 = f.read()
data1 = json.loads(echo2)
f.close()
print(data1)
print(type(data1))
image_id = data1['weather'][0]['icon']
print(image_id)
return
if __name__ == '__main__':
get_current_weather()
get_5_days()
エラー:
./get_weather.py
json files saved to tmp
json is fine
{"coord":{"lon":21.01,"lat":52.23},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"base":"stations","main":{"temp":21,"pressure":1023,"humidity":43,"temp_min":21,"temp_max":21},"visibility":10000,"wind":{"speed":2.6},"clouds":{"all":0},"dt":1502100000,"sys":{"type":1,"id":5374,"message":0.0081,"country":"PL","sunrise":1502075226,"sunset":1502129707},"id":756135,"name":"Warsaw","cod":200}
Traceback (most recent call last):
File "./get_weather.py", line 85, in <module> get_current_weather() File "./get_weather.py", line 27, in get_current_weather get_image('current.json') File "./get_weather.py", line 79, in get_image image_id = data1['weather'][0]['icon'] TypeError: string indices must be integers
[TypeError:stringインデックスは、JSON、Pythonを解析中に整数でなければなりませんか?](https:// stackoverflow。/ code/32229546/typeerror-string-indices-must-be-integer-parsing-json-python) – pkqxdd
そのコードはそのエラーを与えません。投稿されたように、それはうまく動作します。実際のコードと完全な出力(プリントを含む)を示してください。 –
image_id = data1 ['weather']でimage_idを印刷できますか?出力は何ですか? – Toandd