2017-05-28 57 views
0

我對API非常陌生,我正在嘗試製作一個Python腳本來告訴我這個城市的日子很高。我已在API樹立優良的,它輸出的數據是這樣的:使用API​​從相同名稱的多個鍵獲取所有數據python

{ 
    "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的

附:如果需要的話,這是我使用來獲取信息的腳本的副本,它使用氣象局(這是英國一個天氣預報網站)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

至於我可以看到,您的數據嵌套在SiteRep -> DV -> Location -> Period列表中,特別是其Rep元素,因此您只需遍歷它們即可獲取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'} 

甚至:

dms = {p["value"]: p["Rep"][0]["Dm"] for p in data["SiteRep"]["DV"]["Location"]["Period"]} 

print(dms) 
# {'2017-05-29Z': '18', '2017-05-28Z': '22', '2017-05-30Z': '18', 
# '2017-05-31Z': '19', '2017-06-01Z': '19'} 

這是最快的,但它變得很難閱讀...

+0

謝謝,就像一個魅力! – Tom

0

以你的數據,我可以看到它包含:

  • 只有一個鍵,SiteRep其中包含
  • 與會員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']} 
  • DV是密鑰字典dataDatetype & Location
    • `DV ['dataDate']只是'2017-05-28T17:00:00Z',即生成數據時。
    • DV ['location'] is a dictionary with members'i','lat','lon','name','country','continent','elevation','Period',其中大部分是顯而易見的。
    • DV['location']['Period']長度5 表明在5天的列表預測
    • 每個條目是鍵'type', 'value', 'Rep'字典其中&類型值是「日」和分別的日期。
    • 每個DV['location']['Period'][ñ]['Rep']是每個成員'D', 'Gn', 'Hn', 'PPd', 'S', 'V', 'Dm', 'FDm', 'W', 'U', '$'這恰好是data['Wx']['Param']名稱與$除外,它要麼是DayNight

所以基本上2個詞典列表你需要的所有數據都在那裏,你只需要鑽取一點。

已經用於params以上的映射和data['SiteRep']['DV']['Rep'][0]局部變量D0我可以打印:

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         
相關問題