我有这个python
输出,它检索一个字典,如下所示。我想得到TopicArn
的值,它位于NotificationConfiguration
之内
这是我的输出看起来像
clusters
的输出
{
'Marker': 'string',
'CacheClusters': [
{
'CacheClusterId': 'string',
'ConfigurationEndpoint': {
'Address': 'string',
'Port': 123
},
'ClientDownloadLandingPage': 'string',,
'PendingModifiedValues': {
'NumCacheNodes': 123,
'CacheNodeIdsToRemove': [
'string',
],
'EngineVersion': 'string',
'CacheNodeType': 'string',
'AuthTokenStatus': 'SETTING'|'ROTATING'
},
'NotificationConfiguration': {
'TopicArn': 'string',
'TopicStatus': 'string'
},
'ReplicationGroupId': 'string',
},
]
}
这就是我所尝试的:
def get_ec_cache_Arn(region, clusters):
ec_client = boto3.client('elasticache', region_name=region)
count = 1
for cluster in clusters['CacheClusters']:
cluster_arn = cluster['NotificationConfiguration'][0]['TopicArn']
但这不起作用。没有输出。但是clusters
有一个值我正在从其他函数传递。当我打印clusters
时,它生成上面提到的字典。这意味着clusters
不是空的
有人能帮我吗
修改
for
循环。您已经将[0]
放在那里,它将抛出KeyError
,因为在python中,字典没有顺序,您不能对它们使用索引这将提供所需的输出
您的代码中有过多的
[0]
。您已尝试访问元素0
,但“NotificationConfiguration”是字典而不是列表我想分享一个访问字典元素的通用方法:
相关问题 更多 >
编程相关推荐