2017-03-03 39 views
2

我正在從返回JSON的Web調用構建點要素類。 JSON有點簡單,有時鍵不存在於記錄中。我試圖做到這一點,一旦我有一個有效的JSON對象:忽略JSON鍵值對的條件檢查

#requests stuff above this 

for i in jsonObj: 
    try: 
     if i['properties']['country']: 
      country = i['properties']['country'] 
     else: 
      country = 'UNK' 
      print('Country name not found, using {}'.format(country)) 
    except KeyError, e: 
     print('Key error: reason: {}'.format(str(e))) 
     pass 

#then do all the arcpy FC creation stuff 

結果是關鍵的錯誤使用「的理由:‘國家’」一大堆的不是建立與一般的「國家那些行'UNK'的價值,它會簡單地忽略它們並構建要素類,而忽略這些要點。

我已取出try並將其作爲有條件檢查,但在第一行缺少'國家/地區'鍵時失敗。

總之,我只是想檢查一個鍵值對是否存在;如果不是,則將一個通用值'UNK'分配給country變量。

似乎問題的一部分可能是if i['properties']['countries']正在檢查一個值,但不是密鑰本身的存在?我該如何更有效地檢查密鑰的存在?


我已閱讀Check if a given key already exists in a dictionary和修改了我的代碼,以這兩個,並沒有產生預期的結果:

for i in jsonObj: 
    try: 
     # get coordinates first 
     if i['geometry']['coordinates']: 
      ycoord = float(i['geometry']['coordinates'][1]) 
      xcoord = float(i['geometry']['coordinates'][0]) 
      if i['properties']['city'] in i: 
       city = i['properties']['city'] 
      else: 
       city = 'UNK' 

      if i['properties']['country'] in i: 
       country = i['properties']['country'] 
      else: 
       country = 'UNK' 

for i in jsonObj: 
    try: 
     # get coordinates first 
     if i['geometry']['coordinates']: 
      ycoord = float(i['geometry']['coordinates'][1]) 
      xcoord = float(i['geometry']['coordinates'][0]) 
      if 'city' in i: 
       city = i['properties']['city'] 
      else: 
       city = 'UNK' 

      if 'country' in i: 
       country = i['properties']['country'] 
      else: 
       country = 'UNK' 

我有「屬性」鍵入每個記錄/字典,但我是否擁有「國家/地區」密鑰不能保證。在JSON響應中的某些行有它,有些行不

+0

你可以嘗試'如果'屬性'在我:,而不是'如果'國家'在我:'。 –

+0

你在每本字典中都有「屬性」鍵嗎? –

+0

是的,我在每個記錄/字典中都有'屬性'鍵,但我是否擁有'國家/地區'鍵是不能保證的。 json響應中的某些行有它,有些行不。 – auslander

回答

2

你的最後一次嘗試:

if 'country' in i: 
    country = i['properties']['country'] 
else: 
    country = 'UNK' 

接近,但你管理類型的字典詞典,並'country'有更好的機會成爲一個子字典的關鍵,所以修復將是:

if 'country' in i['properties']: 
    country = i['properties']['country'] 
else: 
    country = 'UNK' 

甚至更​​好&使用get用默認值短(我建議過的quickfix最後一個):

country = i['properties'].get('country','UNK') 
+0

轉到.get()方法將成爲我對該腳本的下一個修改,因此非常感謝您對其的瞭解。那麼,那麼,我甚至可以消除甚至做一個if-else檢查,是的? – auslander

+0

是的,不需要檢查,得到它。 –

+0

現在工作很好,謝謝@ Jean-François! – auslander

0

看起來好像你沒有完全理解json的實現。

x = i['geometry']['coordinates']基本上是y = i['geometry']; x = y['coordinates'],所以你需要安全檢查的每一層,becaue i['geometry']不僅會拋出異常,當'geometry'場沒有找到,而且返回的對象也必須實現[]['coordinates']工作(所以在這種情況下,肯定又是json object,而不是stringboolNone等)

我也相信你的JSON對象是使用Python字典實現的,所以您可以檢查是否某些領域,如'geometry'存在使用x.get('geometry'),它將返回其值或None對象。您也可以使用city = x.get('city', 'UKN')設置默認值(json object,string,bool,None等。)如果在dict中沒有找到它(python dict.get方法實現默認處理程序)。那麼到底

,你應該有這樣的事情:

geo = i.get('geometry') 
if not geo: return 

coords = geo.get('coordinates') 
if not coords: return 
xcoord, ycoord = float(coords[0]), float(coords[1]) 

props = i.get('properties') 
if not props: return 
city = props.get('city', 'UNK') 
country = props.get('country', 'UNK') 

這是一個草案,我沒有測試它,這也強烈地斷言,你json object是基於Python的dict object

+0

你是對的,我是json的嬰兒。感謝您的提示。我不在我平常的工作站,但我很樂意在幾天後嘗試這種方式並跟進。 – auslander

+0

今天早上我跑過了它,它的工作方式與上面相同,但是處理更好。謝謝你的提示! – auslander