【发布时间】:2020-03-16 01:04:13
【问题描述】:
我是编程新手,需要有关我编写的 python 代码的帮助。
我想获得我所在城市的 WeatherForecast。我从 Openweathermap 获得了一个 API,它给了我一个 json 文件。现在我想对这个 json 进行排序并将其输出为 excel。
到目前为止,我得到了这段代码(我编辑了 API):
import urllib.request
import json
import pandas
urllib.request.urlretrieve ("http://api.openweathermap.org/data/2.5/forecast?id=2867714&APPID=XXXXXXXXX","WeatherData.json")
with open('WeatherData.json') as f:
data = (line.strip() for line in f)
data_json = "[{0}]".format(','.join(data))
data=json.loads(data_json)
这会生成以下 JSON:
{
"city": {
"coord": {
"lat": 48.1374,
"lon": 11.5755
},
"country": "DE",
"id": 2867714,
"name": "Muenchen",
"sunrise": 1574231244,
"sunset": 1574263882,
"timezone": 3600
},
"cnt": 40,
"cod": "200",
"list": [
{
"clouds": {
"all": 86
},
"dt": 1574251200,
"dt_txt": "2019-11-20 12:00:00",
"main": {
"grnd_level": 941,
"humidity": 71,
"pressure": 1014,
"sea_level": 1014,
"temp": 278.95,
"temp_kf": -0.36,
"temp_max": 279.31,
"temp_min": 278.95
},
"sys": {
"pod": "d"
},
"weather": [
{
"description": "overcast clouds",
"icon": "04d",
"id": 804,
"main": "Clouds"
}
],
"wind": {
"deg": 63,
"speed": 2.86
}
},
{
"clouds": {
"all": 96
},
"dt": 1574262000,
"dt_txt": "2019-11-20 15:00:00",
"main": {
"grnd_level": 940,
"humidity": 85,
"pressure": 1013,
"sea_level": 1013,
"temp": 276.96,
"temp_kf": -0.27,
"temp_max": 277.23,
"temp_min": 276.96
},
"sys": {
"pod": "d"
},
"weather": [
{
"description": "overcast clouds",
"icon": "04d",
"id": 804,
"main": "Clouds"
}
],
"wind": {
"deg": 61,
"speed": 1.98
}
},
我刚刚使用了 2 个列表条目的示例来进行天气预报。 json包含40个。(5天一天8个)
我只需要温度,rel。湿度和压力。其余的对我的应用程序没用。
提前谢谢你
弗洛尔
【问题讨论】: