python - 如何修复 Keyerror:组?
问题描述
我一直在努力处理这段代码,我需要在其中获取有关纽约市印度餐馆的数据,但是我收到错误消息 Keyerror: Groups。我希望有人可以帮我解决这个问题。我将代码与消息一起发布,您可以在其中看到错误。
def get_venues(lat,lng):
#set variables
radius=1000
LIMIT=100
CLIENT_ID = ['FRI4BGBGLDEYD5LXOQC4YHL2JSBGEVH3YZOOWYAYQAER3JT1'] # your Foursquare ID
CLIENT_SECRET = ['Z30R3SJZJ1STXSM0HYE2QCH3KXK2XNJI210NQXOYBHWHOHMO'] # your Foursquare Secret
VERSION = '20200401' # Foursquare API version
#url to fetch data from foursquare api
url = 'https://api.foursquare.com/v2/venues/explore?&client_id={}&client_secret={}&v={}&ll={},{}&radius={}&limit={}'.format(
CLIENT_ID,
CLIENT_SECRET,
VERSION,
lat,
lng,
radius,
LIMIT)
# get all the data
results = requests.get(url).json()
venue_data = results["response"]['groups'][0]['items']
venue_details = []
for row in venue_data:
try:
venue_id=row['venue']['id']
venue_name=row['venue']['name']
venue_category=row['venue']['categories'][0]['name']
venue_details.append([venue_id,venue_name,venue_category])
except KeyError:
pass
column_names=['ID','Name','Category']
df = pd.DataFrame(venue_details,columns=column_names)
return df
# prepare neighborhood list that contains indian resturants
column_names=['Borough', 'Neighborhood', 'ID','Name']
indian_rest_ny=pd.DataFrame(columns=column_names)
count=1
for row in new_york_data.values.tolist():
Borough, Neighborhood, Latitude, Longitude=row
venues = get_venues(Latitude,Longitude)
indian_resturants=venues[venues['Category']=='Indian Restaurant']
print('(',count,'/',len(new_york_data),')','Indian Resturants in '+Neighborhood+', '+Borough+':'+str(len(indian_resturants)))
for resturant_detail in indian_resturants.values.tolist():
id, name , category=resturant_detail
indian_rest_ny = indian_rest_ny.append({'Borough': Borough,
'Neighborhood': Neighborhood,
'ID': id,
'Name' : name
}, ignore_index=True)
count+=1
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-10-7a917332b722> in <module>
5 for row in new_york_data.values.tolist():
6 Borough, Neighborhood, Latitude, Longitude=row
----> 7 venues = get_venues(Latitude,Longitude)
8 indian_resturants=venues[venues['Category']=='Indian Restaurant']
9 print('(',count,'/',len(new_york_data),')','Indian Resturants in '+Neighborhood+', '+Borough+':'+str(len(indian_resturants)))
<ipython-input-3-6da7f127fa47> in get_venues(lat, lng)
21 results = requests.get(url).json()
22 #results = requests.get(url).json()["response"]['groups'][0['items']
---> 23 venue_data = results["response"]['groups'][0]['items']
24 venue_details = []
25 for row in venue_data:
KeyError: 'groups'
解决方案
KeyError
仅当键不在字典中时才会引发A。引发此错误的唯一方法是没有 key Groups
。确保有Groups
,解决问题。
推荐阅读
- go - 如何使用新的 AWS 令牌更新 GORM 中的数据库连接
- android - Kotlin Spinner 选择
- python - How to vectorize advanced indexing with list of lists in NumPy?
- python - python中的芹菜conf
- java - 如何从HashMap中的ArrayList中打印出一个键和一个平均数
- ios - Firebase 检索子嵌套数据
- wireguard - Wireguard - 如何使用 Wireguard VPN 拆分隧道限制对没有静态 IP 的应用程序的 VPN 访问?
- css - 背景图像:url() 没有从 css 文件加载图像
- sas - 如何使用proc sql从sas中的以下数据中获取第一行
- javascript - 如何格式化具有空行的 excel 文件以具有嵌套数组并匹配我需要呈现的 JSON 对象?