В настоящее время получена ключевая ошибка для данных JSON, полученных из API в Python.Что здесь происходит? - PullRequest
1 голос
/ 17 февраля 2011

В настоящее время я получаю "Key Error" в 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"

Вот вывод:1006 *

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'

Любая помощь в том, как исправить эту «ключевую ошибку», будет принята с благодарностью.Я совершенно новичок в обработке JSON в Python.

1 Ответ

6 голосов
/ 17 февраля 2011

Не использовать d.has_key(k);это медленно и не рекомендуется.Используйте k in d.

Не сравнивайте == None или != None, используйте is None или is 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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...