大家好,这是我的第一篇文章。 我为此努力奋斗。 当您要求多天预报时,阵列确实令人头疼 看起来像这样:
{
"city": {
"id": 261779,
"name": "Ioannina",
"coord": {
"lon": 20.8508,
"lat": 39.6675
},
"country": "GR",
"population": 64012,
"timezone": 7200
},
"cod": "200",
"message": 1.6748099,
"cnt": 3,
"list": [
{
"dt": 1581674400,
"sunrise": 1581658280,
"sunset": 1581696629,
"temp": {
"day": 5.85,
"min": 5.34,
"max": 6.89,
"night": 6.73,
"eve": 6.19,
"morn": 5.34
},
"feels_like": {
"day": 3.07,
"night": 5.55,
"eve": 3.59,
"morn": 3.04
},
"pressure": 1019,
"humidity": 97,
"weather": [
{
"id": 502,
"main": "Rain",
"description": "heavy intensity rain",
"icon": "10d"
}
],
"speed": 2.49,
"deg": 197,
"clouds": 100,
"rain": 38.63
},
{
"dt": 1581760800,
"sunrise": 1581744606,
"sunset": 1581783100,
"temp": {
"day": 8.65,
"min": 3.92,
"max": 10.93,
"night": 3.92,
"eve": 9.52,
"morn": 5.04
},
"feels_like": {
"day": 5.27,
"night": 0.66,
"eve": 6.49,
"morn": 1.8
},
"pressure": 1020,
"humidity": 67,
"weather": [
{
"id": 800,
"main": "Clear",
"description": "sky is clear",
"icon": "01d"
}
],
"speed": 2.65,
"deg": 31,
"clouds": 4
},
{
"dt": 1581847200,
"sunrise": 1581830931,
"sunset": 1581869570,
"temp": {
"day": 9.25,
"min": 2.54,
"max": 11.97,
"night": 3.2,
"eve": 10.1,
"morn": 2.62
},
"feels_like": {
"day": 7.09,
"night": 0.06,
"eve": 8.26,
"morn": -0.71
},
"pressure": 1028,
"humidity": 57,
"weather": [
{
"id": 802,
"main": "Clouds",
"description": "scattered clouds",
"icon": "03d"
}
],
"speed": 0.5,
"deg": 59,
"clouds": 31
}
]
}
我有一个星期努力将每天的预测分开。然后在我的程序上打印一些基本值,例如温度和湿度。 另外,我正在使用tkinter构建一个GUI以显示每天的天气预报。'
如果您想查看该程序的代码:https://github.com/zafalex88/open_weather_project/
欢迎大家提出意见。
提前谢谢! 亚历克斯
答案 0 :(得分:0)
您可以打印
print( data['list'][0]['humidity'] )
print( data['list'][1]['humidity'] )
或使用循环
for day in data['list']:
print('humidity:', day['humidity'])
最小的工作示例-它还使用datetime
将dt
(时间戳)转换为日期
import datetime
data = {"city":{"id":261779,"name":"Ioannina","coord":{"lon":20.8508,"lat":39.6675},"country":"GR","population":64012,"timezone":7200},"cod":"200","message":1.6748099,"cnt":3,"list":[{"dt":1581674400,"sunrise":1581658280,"sunset":1581696629,"temp":{"day":5.85,"min":5.34,"max":6.89,"night":6.73,"eve":6.19,"morn":5.34},"feels_like":{"day":3.07,"night":5.55,"eve":3.59,"morn":3.04},"pressure":1019,"humidity":97,"weather":[{"id":502,"main":"Rain","description":"heavy intensity rain","icon":"10d"}],"speed":2.49,"deg":197,"clouds":100,"rain":38.63},{"dt":1581760800,"sunrise":1581744606,"sunset":1581783100,"temp":{"day":8.65,"min":3.92,"max":10.93,"night":3.92,"eve":9.52,"morn":5.04},"feels_like":{"day":5.27,"night":0.66,"eve":6.49,"morn":1.8},"pressure":1020,"humidity":67,"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01d"}],"speed":2.65,"deg":31,"clouds":4},{"dt":1581847200,"sunrise":1581830931,"sunset":1581869570,"temp":{"day":9.25,"min":2.54,"max":11.97,"night":3.2,"eve":10.1,"morn":2.62},"feels_like":{"day":7.09,"night":0.06,"eve":8.26,"morn":-0.71},"pressure":1028,"humidity":57,"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"speed":0.5,"deg":59,"clouds":31}]}
for day in data['list']:
date = datetime.datetime.fromtimestamp(day['dt'])
print('date:', date)
print('humidity:', day['humidity'])
#print('temp:', day['temp'])
print('temp day:', day['temp']['day'])
print('temp min:', day['temp']['min'])
print('temp max:', day['temp']['max'])
结果:
date: 2020-02-14 11:00:00
humidity: 97
temp day: 5.85
temp min: 5.34
temp max: 6.89
date: 2020-02-15 11:00:00
humidity: 67
temp day: 8.65
temp min: 3.92
temp max: 10.93
date: 2020-02-16 11:00:00
humidity: 57
temp day: 9.25
temp min: 2.54
temp max: 11.97
带有tkinter
import tkinter as tk
import datetime
data = {"city":{"id":261779,"name":"Ioannina","coord":{"lon":20.8508,"lat":39.6675},"country":"GR","population":64012,"timezone":7200},"cod":"200","message":1.6748099,"cnt":3,"list":[{"dt":1581674400,"sunrise":1581658280,"sunset":1581696629,"temp":{"day":5.85,"min":5.34,"max":6.89,"night":6.73,"eve":6.19,"morn":5.34},"feels_like":{"day":3.07,"night":5.55,"eve":3.59,"morn":3.04},"pressure":1019,"humidity":97,"weather":[{"id":502,"main":"Rain","description":"heavy intensity rain","icon":"10d"}],"speed":2.49,"deg":197,"clouds":100,"rain":38.63},{"dt":1581760800,"sunrise":1581744606,"sunset":1581783100,"temp":{"day":8.65,"min":3.92,"max":10.93,"night":3.92,"eve":9.52,"morn":5.04},"feels_like":{"day":5.27,"night":0.66,"eve":6.49,"morn":1.8},"pressure":1020,"humidity":67,"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01d"}],"speed":2.65,"deg":31,"clouds":4},{"dt":1581847200,"sunrise":1581830931,"sunset":1581869570,"temp":{"day":9.25,"min":2.54,"max":11.97,"night":3.2,"eve":10.1,"morn":2.62},"feels_like":{"day":7.09,"night":0.06,"eve":8.26,"morn":-0.71},"pressure":1028,"humidity":57,"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"speed":0.5,"deg":59,"clouds":31}]}
root = tk.Tk()
tk.Label(root, text='humidity').pack()
listbox = tk.Listbox(root)
listbox.pack()
for day in data['list']:
#listbox.insert('end', day['humidity'])
date = datetime.datetime.fromtimestamp(day['dt'])
text = '{}: {}'.format(date.strftime('%Y.%m.%d'), day['humidity'])
listbox.insert('end', text)
root.mainloop()