3 回答
TA贡献1869条经验 获得超4个赞
你的支票
if 'AccessKeyId' and 'AccessKeyMetadata[0]':
是一个简单地测试这些常量字符串的检查。在控制台中运行此代码返回:
>>> 'AccessKeyId' and 'AccessKeyMetadata[0]'
'AccessKeyMetadata[0]'
我想你想测试响应对象:
# if response is a string and not yet parsed:
# response = json.loads(response)
# if it is already parsed, go on
# .get() with a default return value [] won't raise a KeyError
# or check with:
# if 'AccessKeyMetadata' in response
meta_data = response.get('AccessKeyMetadata', [])
if len(meta_data) == 1:
key1 = meta_data[0].get('AccessKeyId', None)
elif len(meta_data) > 1:
key2 = meta_data[1].get('AccessKeyId', None)
TA贡献2012条经验 获得超12个赞
您在if子句中的检查有点令人困惑。您正在检查非空字符串,该字符串将始终返回True.
为了使其尽可能接近您当前的代码,您应该这样做:
user_name = str(input("Enter a user name: "))
paginator = client.get_paginator('list_access_keys')
for response in paginator.paginate(UserName=user_name):
print("Raw response: ", response)
if len(response['AccessKeyMetadata']) and 'AccessKeyId' in response['AccessKeyMetadata'][0].keys():
key1 = response['AccessKeyMetadata'][0]['AccessKeyId']
print("Key 1: ", key1)
if len(response['AccessKeyMetadata']) > 1 and 'AccessKeyId' in response['AccessKeyMetadata'][1].keys():
key2 = response['AccessKeyMetadata'][1]['AccessKeyId']
TA贡献1797条经验 获得超6个赞
你的 if 语句没有做你认为他们正在做的事情。例如:
if 'AccessKeyMetadata'
应该:
if response['AccessKeyMetadata']
添加回答
举报