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

2024-06-17 08:11:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个项目,我正在利用Foursquare API从波特兰周边地区获取场馆信息。当我试图从json字典中提取信息并使用我编写的自定义函数将其结构化为数据帧时,我遇到了一个问题。出于某种原因,我编写的函数在字典中的“groups”部分标记了一个keyrerror,即使其中有项,并且字典中也有它。我有一种预感,KeyError可能会被触发,因为字典中的一些组在运行时没有任何项

ex:(查看底部的“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)

(作为旁注,上面的结果中的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

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


Tags: name字典itemsnangroupslnglatportland
1条回答
网友
1楼 · 发布于 2024-06-17 08:11:24

有几种不同的选择

访问dict时,可以在dict上使用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 

相关问题 更多 >