Skip to main content
 首页 » 编程设计

python之当前收到从 Python 中的 API 检索到的 JSON 数据的关键错误。到底是怎么回事

2025年05月04日16tuyile006

我目前在 Python 中收到“关键错误”,我不知道发生了什么。

这是我的代码:

""" This is a program from http://www.masnun.me/2010/01/30/handling-json-in-   python.html which I am 
using to experiment with how to handle JSON data in python, currently the problem is that I have a bunch 
of JSON data returned from the nestoria API, and I don't know how to get what I want out of it. 
""" 
 
import json,urllib 
data = urllib.urlopen("http://api.nestoria.com.au/api?country=au&pretty=1&action=search_listings&encoding=json&listing_type=rent&centre_point=-33.8891,151.1870,3km&number_of_results=30&sort=bedroom_highlow&page=1").read() 
d = json.loads(data) 
for x in d['response']['listings']: 
#check that the necessary data fields we are retriving are not empty and check that  the number of bedrooms is within a reasonable range so we do not divide by zero's or catch typo's 
    if ((int(x['bedroom_number'])!=0)and x['title']!=None and x['latitude'] !=None and x['longitude'] !=None and x['price_high'] !=None): 
        print x['title'], x['latitude'], x['longitude'], x['bedroom_number'], x['price_high'], 'The price per bedroom is... ', (x['price_high'])/int((x['bedroom_number'])) 
 
    #note currently getting a python "key error" 

这是输出:

Flat for rent, Broadway - Lift -33.88440 151.19524 15 100 The price per bedroom is...  6 
Riley Street, Surry Hills - Terrace -33.88462 151.21254 6 2000 The price per bedroom is...  333 
Quay Street, Haymarket - Patio, Lift -33.88184 151.20316 6 2094 The price per bedroom is...  349 
Bourke Street, Darlinghurst - Terrace -33.87921 151.21712 5 1950 The price per bedroom is...  390 
Wigram Road, Glebe -33.87953 151.18179 5 900 The price per bedroom is...  180 
House for rent, Newtown -33.89859 151.17581 5 0 The price per bedroom is...  0 
House to let, Marrickville - Backyard -33.91251 151.16055 5 1200 The price per bedroom is...  240 
Warren Ball Avenue Newtown -33.89426 151.18604 5 2000 The price per bedroom is...  400 
Darling Island Road, Darling Island -33.86879 151.19421 4 2500 The price per bedroom is...  625 
Wellington St, Waterloo - Backyard -33.89860 151.20753 4 850 The price per bedroom is...  212 
Cathedral Street, Woolloomooloo -33.87222 151.21709 4 1000 The price per bedroom is...  250 
Harold Street, Newtown -33.90095 151.18114 4 750 The price per bedroom is...  187 
Jarocin Avenue, Glebe - Terrace -33.88185 151.18430 4 750 The price per bedroom is...  187 
Talfourd Street, Glebe - Fireplace -33.87892 151.18727 4 1200 The price per bedroom is...  300 
Douglas Street Stanmore - Backyard -33.89336 151.16078 4 730 The price per bedroom is...  182 
 
Traceback (most recent call last): 
  File "C:\Users\turing\Documents\Programming Experiments\python\handlingjsonpython.py", line 13, in <module> 
        if ((int(x['bedroom_number'])!=0)and x['title']!=None and x['latitude'] !="" and x['longitude'] !=None and x['price_high'] !=None): 
KeyError: 'latitude' 

任何有关如何修复此“键错误”的帮助将不胜感激。我对在 Python 中处理 JSON 完全陌生。

请您参考如下方法:

不要使用d.has_key(k);它很慢且已弃用。在 d 中使用 k。

不要比较== None或!= None,使用is None或is not None .

不要在 d.get(k, None) 上浪费击键——None 是默认值。使用 d.get(k)

因此,使用 ... 并且 x.get('latitude') 不是 None 并且 ...

Update 刚刚运行该查询,我注意到其中两个结果既没有纬度也没有经度。

我建议您将那个大的 if 语句分解成小块,并避免使用可怕的 x['attribute_name'] 引用数据的方式:

bedroom_number = int(x.get('bedroom_number', '0')) 
latitude = x.get('latitude') 
longitude = x.get('longitude') 
title = x.get('title') 
price_high = x.get('price_high') 
if not (bedroom_number and latitude and longitude and title and price_high): continue 
print title, latitude, longitude, bedroom_number, price_high, \ 
    'The price per bedroom is... ', float(price_high) / bedroom_number