首页 > 解决方案 > 从 api 调用循环遍历列表中的字典

问题描述

我目前正在处理 API 调用。全部由列表中的字典组成。我正在向 OpenWeatherMap 请求天气预报。调用本身没有问题,但是当我想从中提取数据时,它不会循环整个调用。我可以输出的数据总是在列表中的第 4 点之后停止。

这是我循环输出的代码:

n = 0
for items in data:
    data_list = data["list"][n]
    date_and_time = data_list["dt_txt"]
    wind = data_list["wind"]
    temperature = math.floor(data_list["main"]["temp"] -273.15)


    n += 1

    print(date_and_time)
    print(wind)
    print(f"{temperature} C°")
    print(n)

request.json:

{“鳕鱼”:“200”,“消息”:0,“cnt”:16,“列表”:[{“dt”:1609707600,“main”:{“temp”:277.97,“feels_like”:274.32, “temp_min”:277.97,“temp_max”:279.11,“压力”:1012,“sea_level”:1012,“grnd_level”:999,“湿度”:90,“temp_kf”:-1.14 },“天气”:[{ “id”:804,“main”:“Clouds”,“description”:“阴云”,“icon”:“04n”}],“clouds”:{ “all”:86 },“wind”:{ “速度”:3.15,“度”:147},“可见性”:10000,“流行”:0,“系统”:{“pod”:“n”},“dt_txt”:“2021-01-03 21 :00:00" },{“dt”:1609718400,“main”:{“temp”:277.47,“feels_like”:274.42,“temp_min”:277.47,“temp_max”:277.63,“压力”:1013,“sea_level”:1013,“grnd_level “:999,“湿度”:92,“temp_kf”:-0.16},“天气”:[{“id”:804,“主要”:“云”,“描述”:“阴云”,“图标” : "04n" } ], "clouds": { "all": 93 }, "wind": { "speed": 2.25, "deg": 135 }, "visibility": 10000, "pop": 0, " sys”:{“pod”:“n”},“dt_txt”:“2021-01-04 00:00:00”},{“dt”:1609729200,“main”:{“temp”:277.82,“感觉像”:275.22,“temp_min”:277.82,“temp_max”:277.88,“压力”:1013,“sea_level”:1013,“grnd_level”:999,“湿度”:92,“temp_kf”:-0.06},“天气”: [ {“id”:804,“main”:“Clouds”,“description”:“阴云”,“icon”:“04n”}],“clouds”:{“all”:97 },“wind” : { "speed": 1.7, "deg": 111 }, "visibility": 10000, "pop": 0, "sys": { "pod": "n" }, "dt_txt": "2021-01- 04 03:00:00”},{“dt”:1609740000,“main”:{“temp”:277.39,“feels_like”:274,“temp_min”:277.39,“temp_max”:277.39,“压力”:1013 , "sea_level”:1013,“grnd_level”:998,“湿度”:94,“temp_kf”:0 },“weather”:[{“id”:804,“main”:“Clouds”,“description”:“阴天云”,“图标”:“04n”}],“云”:{“所有”:99},“风”:{“速度”:2.79,“度”:112},“能见度”:10000,“流行”:0,“sys”:{“pod”:“n”},“dt_txt”:“2021-01-04 06:00:00”},{“dt”:1609750800,“main”:{“ temp”:278.16,“feels_like”:275.52,“temp_min”:278.16,“temp_max”:278.16,“压力”:1014,“sea_level”:1014,“grnd_level”:999,“湿度”:92,“temp_kf” : 0 },“天气”:[{“id”:500,“main”:“Rain”,“description”:“小雨”,“icon”:“10d”}],“clouds”:{“all”:100} ,“风”:{“速度”:1.84,“度”:87},“能见度”:10000,“流行”:0.23,“雨”:{“3h”:0.32},“系统”:{“吊舱": "d" }, "dt_txt": "2021-01-04 09:00:00" }, { "dt": 1609761600, "main": { "temp": 280.2, "feels_like": 276.8, " temp_min”:280.2,“temp_max”:280.2,“压力”:1013,“sea_level”:1013,“grnd_level”:998,“湿度”:83,“temp_kf”:0 },“天气”:[{“id ”:804,“主要”:“云”,“描述”:“阴云”,“图标”:“04d”}],“云”:{“全部”:98 },“风”:{“速度”:3.07,“度”: 108 },“可见性”:10000,“流行”:0.34,“sys”:{“pod”:“d”},“dt_txt”:“2021-01-04 12:00:00”},{“dt ”:1609772400,“main”:{“temp”:279.71,“feels_like”:276.09,“temp_min”:279.71,“temp_max”:279.71,“压力”:1012,“sea_level”:1012,“grnd_level”:998 ,“湿度”:85,“temp_kf”:0 },“天气”:[ {“id”:804,“main”:“云”,“描述”:“阴云”,“图标”:“04d" } ], "clouds": { "all": 99 }, "wind": { "speed": 3.35, "deg": 94 }, "visibility": 10000, "pop": 0.03, "sys" :{“pod”:“d”},“dt_txt”:“2021-01-04 15:00:00”},{“dt”:1609783200,“main”:{“temp”:277.7,“feels_like” :275.22,“temp_min”:277.7,“temp_max”:277.7,“压力”:1012,“sea_level”:1012,“grnd_level”:997,“湿度”:90,“temp_kf”:0 },“天气”: [ {“id”:804,“main”:“Clouds”,“description”:“阴云”,“icon”:“04n”}],“clouds”:{“all”:96 },“wind” :{“速度”:1.41,“度”:102},“可见性”:10000,“流行”:0.03,“sys”:{“pod”:“n”},“dt_txt”:“2021-01-04 18:00:00”} ,{“dt”:1609794000,“main”:{“temp”:276.88,“feels_like”:274.84,“temp_min”:276.88,“temp_max”:276.88,“压力”:1012,“sea_level”:1012,“ grnd_level”:997,“湿度”:92,“temp_kf”:0 },“天气”:[ {“id”:804,“main”:“云”,“描述”:“阴云”,“图标” : "04n" } ], "clouds": { "all": 99 }, "wind": { "speed": 0.66, "deg": 78 }, "visibility": 10000, "pop": 0, "系统“:{”pod”:“n”},“dt_txt”:“2021-01-04 21:00:00”},{“dt”:1609804800,“main”:{“temp”:276.64,“feels_like”:274.21, “temp_min”:276.64,“temp_max”:276.64,“压力”:1011,“sea_level”:1011,“grnd_level”:997,“湿度”:92,“temp_kf”:0 },“天气”:[{“ id”:804,“main”:“Clouds”,“description”:“阴云”,“icon”:“04n”}],“clouds”:{“all”:100},“wind”:{“ speed": 1.15, "deg": 278 }, "visibility": 10000, "pop": 0, "sys": { "pod": "n" }, "dt_txt": "2021-01-05 00: 00:00" }, { "dt":1609815600,“main”:{“temp”:276.48,“feels_like”:273.97,“temp_min”:276.48,“temp_max”:276.48,“压力”:1011,“sea_level”:1011,“grnd_level”:996,“湿度”:91,“temp_kf”:0},“天气”:[{“id”:804,“main”:“云”,“描述”:“阴云”,“图标”:“04n”}] ,“云”:{“所有”:100},“风”:{“速度”:1.2,“度”:4},“能见度”:10000,“流行”:0,“系统”:{“吊舱” ": "n" }, "dt_txt": "2021-01-05 03:00:00" }, { "dt": 1609826400, "main": { "temp": 275.87, "feels_like": 272.41, "temp_min”:275.87,“temp_max”:275.87,“压力”:1011,“sea_level”:1011,“grnd_level”:996,“湿度”:92,“temp_kf”:0 },“天气”:[{“id ": 804, "main": "Clouds", "description": "阴云", "icon": "04n" } ], "clouds": { "all": 100 }, "wind": { "speed “:2.45,“度”:310},“可见性”:10000,“流行”:0,“sys”:{“pod”:“n”},“dt_txt”:“2021-01-05 06:00 :00" }, { "dt": 1609837200, "main": { "temp": 277.33, "feels_like": 273.56, "temp_min": 277.33, "temp_max": 277.33, "pressure": 1012, "sea_level":1012,“grnd_level”:997,“湿度”:85,“temp_kf”:0},“天气”:[{“id”:804,“main”:“云”,“描述”:“阴云” , "icon": "04d" } ], "clouds": { "all": 100 }, "wind": { "speed": 2.97, "deg": 300 }, "visibility": 10000, "pop" :0,“sys”:{“pod”:“d”},“dt_txt”:“2021-01-05 09:00:00”},{“dt”:1609848000,“main”:{“temp” :279.21,“feels_like”:275.72,“temp_min”:279.21,“temp_max”:279.21,“压力”:1011,“sea_level”:1011,“grnd_level”:996,“湿度”:75,“temp_kf”:0 }, "天气”:[{“id”:804,“主要”:“云”,“描述”:“阴云”,“图标”:“04d”}],“云”:{“全部”:100}, “风”:{“速度”:2.59,“度”:312},“能见度”:10000,“流行”:0,“系统”:{“吊舱”:“d”},“dt_txt”:“2021 -01-05 12:00:00”},{“dt”:1609858800,“main”:{“temp”:277.73,“feels_like”:275.17,“temp_min”:277.73,“temp_max”:277.73,“压力“:1011,“海平面”:1011,“grnd_level”:997,“湿度”:81,“temp_kf”:0},“天气”:[{“id”:803,“main”:“云”,“描述”:“破云”,“图标”:“04d”}],“云”:{“所有”:65 },“风”:{“速度”:1.17,“度”:305 },“能见度”:10000 ,“pop”:0,“sys”:{“pod”:“d”},“dt_txt”:“2021-01-05 15:00:00”},{“dt”:1609869600,“main”: { “temp”:277.52,“feels_like”:275.65,“temp_min”:277.52,“temp_max”:277.52,“压力”:1012,“sea_level”:1012,“grnd_level”:998,“湿度”:80,“ temp_kf": 0 }, "weather": [ { "id": 803, "main": "Clouds", "description": "破云", "icon": "04n" } ], "clouds": { “全部”:65 },“风”:{“速度”:0.1,“度”:166 },“能见度”:10000,“流行”:0,“sys”:{“pod”:“n”},“dt_txt” : "2021-01-05 18:00:00" } ], "city": { "id": 2768622, "name": "Podersdorf am See", "coord": { "lat": 47.8541, "lon “:16.8371 },“国家”:“AT”,“人口”:1000,“时区”:3600,“日出”:1609656103,“日落”:1609686737 } }名称”:“Podersdorf am See”,“坐标”:{“lat”:47.8541,“lon”:16.8371},“国家”:“AT”,“人口”:1000,“时区”:3600,“日出” :1609656103,“日落”:1609686737 } }名称”:“Podersdorf am See”,“坐标”:{“lat”:47.8541,“lon”:16.8371},“国家”:“AT”,“人口”:1000,“时区”:3600,“日出” :1609656103,“日落”:1609686737 } }

感谢您的时间!

标签: pythonjsonlistapidictionary

解决方案


你迭代了错误的元素——data它是字典,它只有 4 个项目——"cod", "message", "cnt", "list"你用来n从不同的元素获取值data["list"][n]——它有更多的元素。

你应该迭代data['list']而不是data- 你不需要n这个。

data = {....}

for item in data["list"]:
    date_and_time = item["dt_txt"]
    wind = item["wind"]
    temperature = math.floor(item["main"]["temp"] - 273.15)

    print(date_and_time)
    print(wind)
    print(temperature, "°C")

结果:

2021-01-03 21:00:00
{'speed': 3.15, 'deg': 147}
4 °C
2021-01-04 00:00:00
{'speed': 2.25, 'deg': 135}
4 °C
2021-01-04 03:00:00
{'speed': 1.7, 'deg': 111}
4 °C
2021-01-04 06:00:00
{'speed': 2.79, 'deg': 112}
4 °C
2021-01-04 09:00:00
{'speed': 1.84, 'deg': 87}
5 °C
2021-01-04 12:00:00
{'speed': 3.07, 'deg': 108}
7 °C
2021-01-04 15:00:00
{'speed': 3.35, 'deg': 94}
6 °C
2021-01-04 18:00:00
{'speed': 1.41, 'deg': 102}
4 °C
2021-01-04 21:00:00
{'speed': 0.66, 'deg': 78}
3 °C
2021-01-05 00:00:00
{'speed': 1.15, 'deg': 278}
3 °C
2021-01-05 03:00:00
{'speed': 1.2, 'deg': 4}
3 °C
2021-01-05 06:00:00
{'speed': 2.45, 'deg': 310}
2 °C
2021-01-05 09:00:00
{'speed': 2.97, 'deg': 300}
4 °C
2021-01-05 12:00:00
{'speed': 2.59, 'deg': 312}
6 °C
2021-01-05 15:00:00
{'speed': 1.17, 'deg': 305}
4 °C
2021-01-05 18:00:00
{'speed': 0.1, 'deg': 166}
4 °C

推荐阅读