我是API的新手,我正在尝试制作一个Python脚本来告诉我我所在城市的白天时间。我把API设置得很好,它输出的数据是:
{
"SiteRep":{
"Wx":{
"Param":[
{
"name":"FDm",
"units":"C",
"$":"Feels Like Day
Maximum Temperature"
},
{
"name":"FNm",
"units":"C",
"$":"Feels Like Night Minimum Temperature"
},
{
"name":"Dm",
"units":"C",
"$":"Day Maximum Temperature"
},
{
"name":"Nm",
"units":"C",
"$":"Night Minimum Temperature"
},
{
"name":"Gn",
"units":"mph",
"$":"Wind Gust Noon"
},
{
"name":"Gm",
"units":"mph",
"$":"Wind Gust Midnight"
},
{
"name":"Hn",
"units":"%",
"$":"Screen Relative Humidity Noon"
},
{
"name":"Hm",
"units":"%",
"$":"Screen Relative Humidity Midnight"
},
{
"name":"V",
"units":"",
"$":"Visibility"
},
{
"name":"D",
"units":"compass",
"$":"Wind Direction"
},
{
"name":"S",
"units":"mph",
"$":"Wind Speed"
},
{
"name":"U",
"units":"",
"$":"Max UV Index"
},
{
"name":"W",
"units":"",
"$":"Weather Type"
},
{
"name":"PPd",
"units":"%",
"$":"Precipitation Probability Day"
},
{
"name":"PPn",
"units":"%",
"$":"Precipitation Probability Night"
}
]
},
"DV":{
"dataDate":"2017-05-28T17:00:00Z",
"type":"Forecast",
"Location":{
"i":"350896",
"lat":"50.8371",
"lon":"-0.7734",
"name":"LONDON",
"country":"ENGLAND",
"continent":"EUROPE",
"elevation":"14.0",
"Period":[
{
"type":"Day",
"value":"2017-05-28Z",
"Rep":[
{
"D":"SE",
"Gn":"9",
"Hn":"66",
"PPd":"51",
"S":"7",
"V":"GO",
"Dm":"22",
"FDm":"20",
"W":"7",
"U":"6",
"$":"Day"
},
{
"D":"ENE",
"Gm":"20",
"Hm":"85",
"PPn":"42",
"S":"9",
"V":"VG",
"Nm":"16",
"FNm":"15",
"W":"12",
"$":"Night"
}
]
},
{
"type":"Day",
"value":"2017-05-29Z",
"Rep":[
{
"D":"SW",
"Gn":"13",
"Hn":"91",
"PPd": "59",
"S":"9",
"V":"GO",
"Dm":"18",
"FDm":"16",
"W":"12",
"U":"5",
"$":"Day"
},
{
"D":"W",
"Gm":"13",
"Hm":"93",
"PPn":"7",
"S":"7",
"V":"GO",
"Nm":"13",
"FNm":"12",
"W":"7",
"$":"Night"
}
]
},
{
"type":"Day",
"value":"2017-05-30Z",
"Rep":[
{
"D":"WSW",
"Gn":"25",
"Hn":"75",
"PPd":"9",
"S":"13",
"V":"VG",
"Dm":"18",
"FDm":"15",
"W":"7",
"U":"5",
"$":"Day"
},
{
"D":"WNW",
"Gm":"13",
"Hm":"89",
"PPn":"5",
"S":"9",
"V":"VG",
"Nm":"11",
"FNm":"10",
"W":"0",
"$":"Night"
}
]
},
{
"type":"Day",
"value":"2017-05-31Z",
"Rep":[
{
"D":"SW",
"Gn":"13",
"Hn":"58",
"PPd":"3",
"S":"4",
"V":"VG",
"Dm":"19",
"FDm":"18",
"W":"1",
"U":"7",
"$":"Day"
},
{
"D":"NE",
"Gm":"9",
"Hm":"87",
"PPn":"2",
"S":"4",
"V":"VG",
"Nm":"10",
"FNm":"9",
"W":"0",
"$":"Night"
}
]
},
{
"type":"Day",
"value":"2017-06-01Z",
"Rep":[
{
"D":"ESE",
"Gn":"18",
"Hn":"56",
"PPd":"0",
"S":"9",
"V":"VG",
"Dm":"19",
"FDm":"18",
"W":"1",
"U":"8",
"$":"Day"
},
{
"D":"E",
"Gm":"13",
"Hm":"80",
"PPn":"1",
"S":"9",
"V":"VG",
"Nm":"11",
"FNm":"9",
"W":"0",
"$":"Night"
}
]
}
]
}
}
}
}
问题是我想要打印出所有日间高单位,这些是字典中的Dm键,但我无法将所有单独词典中的键对应的所有数据收集到列表中(或其他任何内容) 。在任何数据类型中收集它们的任何帮助都会非常有用。我的目标是打印出与它们对应的白天高点的日期。
感谢您阅读(和帮助), API的菜鸟
P.S。如果需要,这是我用来获取信息的脚本的副本,它使用的是Met Office(这是英国的天气预报站点)API:
import urllib.request, json, codecs
url = 'Insert API here'
json_obj = urllib.request.urlopen(url)
reader = codecs.getreader("utf-8")
data = json.load(reader(json_obj))
print (data)
答案 0 :(得分:0)
据我所知,您的数据嵌套在SiteRep -> DV -> Location -> Period
列表中,特别是其Rep
元素,因此您只需要遍历它们,然后选择{{1}值和存储在列表中,存储为字典,其中键是每个句点Dm
。类似的东西:
value
如果dms = {} # use this dict for storage
for period in data["SiteRep"]["DV"]["Location"]["Period"]: # loop through periods
# use Period's 'value' as a key; pick up Dm values and store them as value, as a list
dms[period["value"]] = [v["Dm"] for v in period["Rep"] if "Dm" in v]
print(dms)
# {'2017-05-29Z': ['18'], '2017-05-28Z': ['22'], '2017-05-30Z': ['18'],
# '2017-05-31Z': ['19'], '2017-06-01Z': ['19']}
永远不会出现在Dm
的第二个列表元素中(仅限单个值),您可以进一步将其简化为:
Rep
甚至:
dms = {} # use this dict for storage
for period in data["SiteRep"]["DV"]["Location"]["Period"]: # loop through periods
# use Period's 'value' as a key; pick up Dm values and store them as value, as a list
dms[period["value"]] = period["Rep"][0]["Dm"]
print(dms)
# {'2017-05-29Z': '18', '2017-05-28Z': '22', '2017-05-30Z': '18',
# '2017-05-31Z': '19', '2017-06-01Z': '19'}
这是最快但却难以阅读......
答案 1 :(得分:0)
获取您的数据我可以看到它包含:
Wx
&成员的词典DV
Wx
是一个包含一个键Param
和Param
是一个词典列表,每个词典都包含成员:$
,name
& units
其中$
是描述性文字。 就个人而言,我会尝试将其重组为字典词典,其名称为键
data['SiteRep']['Wx']['Param'][0] ==> {
'$': 'Feels Like Day Maximum Temperature',
'name': 'FDm',
'units': 'C'}
但这可以用以下方式映射:
params = {i['name']:(i['$'], i['units']) for i in data['SiteRep']['Wx']['Param']}
dataDate
,type
&密钥的字典。 Location
is a dictionary with members
' i',' lat',' lon', ' name',' country',' continent',' elevation',' Period'`其中大部分是显而易见的DV['location']['Period']
长度为5 的列表,提示5天的预测 'type', 'value', 'Rep'
的字典,其中type&价值是' Day'和分别约会。DV['location']['Period'][
n ]['Rep']
是一个包含2个字典的列表,每个字典都带有成员'D', 'Gn', 'Hn', 'PPd', 'S', 'V', 'Dm', 'FDm', 'W', 'U', '$'
,恰好是data['Wx']['Param']
中的名称例外$
或Day
Night
例外
所以基本上你需要的所有数据都只需要深入了解。
使用上面params
的映射和D0
的本地变量data['SiteRep']['DV']['Rep'][0]
我可以打印:
print('Date:', d0['value'])
for f in d0['Rep']:
print(f['$'])
for k,v in f.items():
if k in params:
print(params[k][0], v, params[k][1])
得到:
Date: 2017-05-28Z
Day
Wind Direction SE compass
Wind Gust Noon 9 mph
Screen Relative Humidity Noon 66 %
Precipitation Probability Day 51 %
Wind Speed 7 mph
Visibility GO
Day Maximum Temperature 22 C
Feels Like Day Maximum Temperature 20 C
Weather Type 7
Max UV Index 6
Night
Wind Direction ENE compass
Wind Gust Midnight 20 mph
Screen Relative Humidity Midnight 85 %
Precipitation Probability Night 42 %
Wind Speed 9 mph
Visibility VG
Night Minimum Temperature 16 C
Feels Like Night Minimum Temperature 15 C
Weather Type 12