当前接收到Python中从API检索的JSON数据的关键错误。发生了什么事?

当前接收到Python中从API检索的JSON数据的关键错误。发生了什么事?,python,json,Python,Json,我目前在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

我目前在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'
百老汇出租公寓-电梯-33.88440151.19524 15100每间卧室的价格是。。。6.
Surry Hills Riley Street-Terrace-33.88462 151.21254 6 2000每间卧室的价格是。。。333
码头街,干草市场-露台,电梯-33.88184 151.20316 6 2094每间卧室的价格是。。。349
伯克街,达林赫斯特-露台-33.87921 151.21712 5 1950每间卧室的价格是。。。390
格里布威格拉姆路-33.87953 151.18179 5900每间卧室的价格是。。。180
出租房屋,纽顿-33.89859 151.17581 5 0每间卧室的价格是。。。0
出租房屋,Marrickville-后院-33.91251 151.16055 5 1200每间卧室的价格是。。。240
沃伦鲍尔大街纽顿-33.89426151.186045 2000每间卧室的价格是。。。400
达令岛达令岛路-33.86879 151.19421 4 2500每间卧室的价格是。。。625
威灵顿街,滑铁卢-后院-33.89860151.207534850每间卧室的价格是。。。212
伍尔鲁穆鲁大教堂街-33.87222151.21709 4 1000每间卧室的价格是。。。250
纽敦哈罗德街-33.90095 151.18114 4750每间卧室的价格是。。。187
格勒布贾罗辛大道-露台-33.88185151.184304750每间卧室的价格是。。。187
格莱布塔尔福德街-壁炉-33.87892 151.18727 4 1200每间卧室的价格是。。。300
道格拉斯街斯坦莫尔-后院-33.89336151.16078 4730每间卧室的价格是。。。182
回溯(最近一次呼叫最后一次):
文件“C:\Users\turing\Documents\Programming Experiments\python\handlingjsonpython.py”,第13行,在
如果((int(x[‘卧室号码’)!=0)和x[‘头衔’!=None和x[‘纬度’!=”“和x[‘经度’!=None和x[‘价格高’!=None):
KeyError:“纬度”
任何关于如何修复此“关键错误”的帮助都将不胜感激。我对用Python处理JSON一无所知。

不要使用
d.has_key(k)
;它很慢而且不受欢迎。在d中使用
k

不要比较
==None
!=无
,使用
是无
不是无

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

所以,使用
。。。并且x.get('latitude')不是None并且…

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

我建议你把那句大的
if
语句分解成小块,并避免可怕的
x['attribute\u 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
不要使用
d.has_键(k)
;它很慢而且不受欢迎。在d中使用
k

不要比较
==None
!=无
,使用
是无
不是无

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

所以,使用
。。。并且x.get('latitude')不是None并且…

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

我建议你把那句大的
if
语句分解成小块,并避免可怕的
x['attribute\u 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