【问题标题】:python - How to resolve KeyError when pulling from a json dictionarypython - 从json字典中提取时如何解决KeyError
【发布时间】:2020-02-11 23:35:08
【问题描述】:

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

例如:(查看底部的“项目”子组,您会看到有一个空列表。我的函数正在该子组中查找信息,所以这可能是标记错误的原因,)

{'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)

(作为旁注,结果中的 0 是什么拉上 (['responses']['groups'][0]['items]) 到底在做什么?从我的课程中借用这部分代码正在努力。

---------------------------------------------------------------------------
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

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

【问题讨论】:

  • 出于某种原因,我编写的函数在字典的“组”部分标记了一个 KeyError,即使其中有项目并且它存在于字典中。我有一种预感,KeyError 可能是由于字典中的某些组在其中没有任何项目而触发的。 KeyError 不应该是由列表是否为空引起的。您需要拆分results = requests.get(url, "none").json()['response']['groups'][0]['items'] 行,并检查每个步骤的结果。

标签: python pandas dataframe dictionary keyerror


【解决方案1】:

对此有几种不同的选择。

访问字典时,您可以在字典上使用get 函数。此函数提取值,但也允许您设置默认值。

results = requests.get(url, "none").json().get('response', {})('groups', [])

Python 没有列表的等价物,因此在这种情况下,您将不得不编写一些 if 语句来查看列表是否有要提取的元素。

另一种选择是添加更多检查并在项目不存在时尽快退出。

jsonBlob = requests.get(url, "none").json()
if 'response' not in jsonBlob or 'groups' not in jsonBlob['response']:
    return False 

【讨论】:

    猜你喜欢
    • 2021-12-02
    • 2016-10-08
    • 1970-01-01
    • 1970-01-01
    • 2016-05-25
    • 1970-01-01
    • 1970-01-01
    • 2018-03-08
    相关资源
    最近更新 更多