在Python中从API获取JSON数据时出现键错误,怎么回事?

1 投票
1 回答
3049 浏览
提问于 2025-04-16 11:59

我现在在用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完全是新手。

1 个回答

6

不要使用 d.has_key(k);这个方法慢,而且已经不推荐使用了。应该用 k in d

不要用 == None!= None 来比较,应该用 is Noneis not None

不要浪费时间在 d.get(k, None) 上,因为 None 是默认值。直接用 d.get(k) 就可以了。

所以,应该用 ... and x.get('latitude') is not None and ...

更新:刚刚运行那个查询时,我发现有两个结果既没有纬度也没有经度。

我建议你把那个大的 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

撰写回答