python-从json字典中提取时如何解决KeyError

问题描述 投票:0回答:1

我正在一个项目中,我正在使用Foursquare API从波特兰周围的位置提取场地信息。尝试从json字典中提取信息并使用我编写的自定义函数将其结构化为数据框时,遇到了问题。由于某种原因,我编写的函数在字典的“组”部分中标记了一个KeyError,即使其中包含项并且它存在于字典中。我有一种预感,由于字典中的某些组在其中没有任何项目时,可能会触发KeyError。

例如:(看一下底部的'items'子组,您会看到一个空列表。我的功能是在该子组中寻找信息,所以也许这就是标记错误的原因,]]]

{'meta': {'code': 200, 'requestId': '5e4330219fcb92001bd8a847'},
 'response': {'warning': {'text': 'There aren\'t a lot of results for "(\'Bookstore\', \'Comic Shop\', \'Used Bookstore\', \'Library\')." Try something more general, reset your filters, or expand the search area.'},
  'headerLocation': 'University Park',
  'headerFullLocation': 'University Park, Portland',
  'headerLocationGranularity': 'neighborhood',
  'query': 'bookstore comic shop used bookstore library',
  'totalResults': 0,
  'suggestedBounds': {'ne': {'lat': 45.5830000045, 'lng': -122.72538279609336},
   'sw': {'lat': 45.573999995499996, 'lng': -122.73821720390666}},
  'groups': [{'type': 'Recommended Places',
    'name': 'recommended',
    'items': []}]}}

与此相比,这就是来自API请求的“普通”字典的外观:

{'meta': {'code': 200, 'requestId': '5e4335769da7ee001b203b56'},
 'response': {'warning': {'text': 'There aren\'t a lot of results for "(\'Bookstore\', \'Comic Shop\', \'Used Bookstore\', \'Library\')." Try something more general, reset your filters, or expand the search area.'},
  'headerLocation': 'Cathedral Park',
  'headerFullLocation': 'Cathedral Park, Portland',
  'headerLocationGranularity': 'neighborhood',
  'query': 'bookstore comic shop used bookstore library',
  'totalResults': 2,
  'suggestedBounds': {'ne': {'lat': 45.5928000045, 'lng': -122.7515716757994},
   'sw': {'lat': 45.583799995499994, 'lng': -122.76440832420062}},
  'groups': [{'type': 'Recommended Places',
    'name': 'recommended',
    'items': [{'reasons': {'count': 0,
       'items': [{'summary': 'This spot is popular',
         'type': 'general',
         'reasonName': 'globalInteractionReason'}]},
      'venue': {'id': '5632a8dc498e5636877e4fa3',
       'name': 'Comic Cave PDX',
       'location': {'address': '1920 N. Kirkpatrick',
        'crossStreet': 'Denver',
        'lat': 45.59002437301252,
        'lng': -122.75552067488051,
        'labeledLatLngs': [{'label': 'display',
          'lat': 45.59002437301252,
          'lng': -122.75552067488051}],
        'distance': 271,
        'postalCode': '97217',
        'cc': 'US',
        'city': 'Portland',
        'state': 'OR',
        'country': 'United States',
        'formattedAddress': ['1920 N. Kirkpatrick (Denver)',
         'Portland, OR 97217',
         'United States']},
       'categories': [{'id': '52f2ab2ebcbc57f1066b8b18',
         'name': 'Comic Shop',
         'pluralName': 'Comic Shops',
         'shortName': 'Comic Shop',
         'icon': {'prefix': 'https://ss3.4sqi.net/img/categories_v2/shops/comic_',
          'suffix': '.png'},
         'primary': True}],
       'photos': {'count': 0, 'groups': []}},
      'referralId': 'e-0-5632a8dc498e5636877e4fa3-0'},
     {'reasons': {'count': 0,
       'items': [{'summary': 'This spot is popular',
         'type': 'general',
         'reasonName': 'globalInteractionReason'}]},
      'venue': {'id': '5d617b8e228cfc0008fb1f9f',
       'name': 'Two Rivers Bookstore',
       'location': {'address': '8836 N Lombard St',
        'lat': 45.591434,
        'lng': -122.75648600000001,
        'labeledLatLngs': [{'label': 'display',
          'lat': 45.591434,
          'lng': -122.75648600000001}],
        'distance': 368,
        'postalCode': '97203',
        'cc': 'US',
        'city': 'Portland',
        'state': 'OR',
        'country': 'United States',
        'formattedAddress': ['8836 N Lombard St',
         'Portland, OR 97203',
         'United States']},
       'categories': [{'id': '4bf58dd8d48988d114951735',
         'name': 'Bookstore',
         'pluralName': 'Bookstores',
         'shortName': 'Bookstore',
         'icon': {'prefix': 'https://ss3.4sqi.net/img/categories_v2/shops/bookstore_',
          'suffix': '.png'},
         'primary': True}],
       'photos': {'count': 0, 'groups': []}},
      'referralId': 'e-0-5d617b8e228cfc0008fb1f9f-1'}]}]}}

我最近添加了一个Except子句,该子句允许函数执行,但是当我使用它时,每个项目都被标记为异常(即使应该在字典中包含项目的请求项也是如此)。我的代码和错误如下:

我的代码:

def getNearbyVenues(names, latitudes, longitudes, limit=500):

    venues_list=[]
    for name, lat, lng in zip(names, latitudes, longitudes):
        print(name)
        url = 'https://api.foursquare.com/v2/venues/explore?&client_id={}&client_secret={}&ll={},{}&v={}&query={}&radius={}&limit={}'.format(
            CLIENT_ID, 
            CLIENT_SECRET, 
            VERSION,
            lat,
            lng,
            query, 
            radius, 
            LIMIT)

        try:
        results = requests.get(url, "none").json()['response']['groups'][0]['items']


            venues_list.extend([(
                name, 
                lat, 
                lng, 
                v['venue']['name'], 
                v['venue']['location']['lat'], 
                v['venue']['location']['lng'],  
                v['venue']['categories'][0]['name']) for v in results])

        except KeyError:
            venues_list.extend([(
               name,
               lat,
               lng,
               np.nan,
               np.nan,
               np.nan,
               np.nan)]) 

    nearby_venues = pd.DataFrame(venues_list, columns = ['Neighborhood', 
                  'Neighborhood Latitude', 
                  'Neighborhood Longitude', 
                  'Venue', 
                  'Venue Latitude', 
                  'Venue Longitude', 
                  'Venue Category'])

    return(nearby_venues)

(作为旁注,结果在上面的[[responses'] ['groups'] [0] ['items])中的0到底在做什么?我正在努力。

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-16-66be6cf8c7d3> in <module>
      1 PDX_venues = getNearbyVenues(names=PDX_NeighDF['Neighborhood'],
      2                              latitudes=PDX_NeighDF['Latitude'],
----> 3                              longitudes=PDX_NeighDF['Longitude']
      4                              )
      5 

<ipython-input-15-dab42c6d540d> in getNearbyVenues(names, latitudes, longitudes, limit)
     15 
     16         #try:
---> 17         results = requests.get(url, "none").json()['response']['groups'][0]['items']
     18 
     19 

KeyError: 'groups'

这是试图构建数据框的功能:

PDX_venues = getNearbyVenues(names=PDX_NeighDF['Neighborhood'],
                             latitudes=PDX_NeighDF['Latitude'],
                             longitudes=PDX_NeighDF['Longitude']
                             )

PDX_venues.head()

output: (structured in a pandas df normally)

    Neighborhood    Neighborhood Latitude   Neighborhood Longitude  Venue   Venue Latitude  Venue Longitude Venue Category
0   CATHEDRAL PARK  45.58830    -122.75799  NaN NaN NaN NaN
1   UNIVERSITY PARK 45.57850    -122.73180  NaN NaN NaN NaN
2   PIEDMONT    45.56510    -122.66820  NaN NaN NaN NaN
3   WOODLAWN    45.56970    -122.65240  NaN NaN NaN NaN
4   ARBOR LODGE 45.57354    -122.69240  NaN NaN NaN NaN

非常感谢您的帮助!如果您需要有关代码的更多信息或背景,请告诉我,我们将乐意提供更多信息。

我正在一个项目中,我正在使用Foursquare API从波特兰周围的位置提取场地信息。尝试从json中提取信息时遇到问题...

python pandas dataframe dictionary keyerror
1个回答
0
投票

有一些不同的选择。

访问词典时,可以在词典上使用get功能。此函数提取值,但也可以设置默认值。

© www.soinside.com 2019 - 2024. All rights reserved.