更好地理解我为什么会出现Key错误


iamfinder = boto3.client('iam')
response = iamfinder.list_roles()
print(response["RoleName"])

我想在给定的帐户中按名称筛选角色,并返回一个列表。目前,如果我排除";RoleName";。为什么我在运行上述程序时看到Key错误?

这是因为响应没有RoleName密钥。应该是{'Roles: [{ ..., 'RoleName': ..., }] }。你能试试这个吗?

import boto3
iamfinder = boto3.client('iam')
response = iamfinder.list_roles()
Role_list = response['Roles']
for key in Role_list:
print(key['RoleName'])

最新更新