【问题标题】:How to access key and value from this dictionary如何从此字典中访问键和值
【发布时间】:2022-11-03 09:45:21
【问题描述】:

这是 Python 中的函数代码的 sn-p:

def restart_lab_server(aws_client):

    response = aws_client.describe_instances()
    ###print(response)
    for reservation in response["Reservations"]:
        for instance in reservation["Instances"]:
            # This sample print will output entire Dictionary object

            # This will print will output the value of the Dictionary key 'InstanceId'
            try:
                print(instance["Tags"])
            except:
                print("This instance does not have any tags.")
                print("Next Entry.")


    return 0

我得到的输出如下:

[{'Key': 'Name', 'Value': 'EC2-Nessus-AmazonLinux2'}]
[{'Key': 'Name', 'Value': 'LCE Server'}]
[{'Key': 'Name', 'Value': 'Windows-2019-xxxxx'}]
[{'Key': 'Name', 'Value': 'SecurityCenter-Lab-5.19.1'}]
[{'Key': 'Name', 'Value': 'Nessus-Target-Lab'}]
[{'Key': 'Name', 'Value': 'Nessus-Agent-Lab'}]
[{'Key': 'Name', 'Value': 'NNM-Lab'}]
[{'Key': 'Name', 'Value': 'WindowsInstance'}]
This instance does not have any tags.
Next Entry.
[{'Key': 'CfStackId', 'Value': 'arn:aws:cloudformation:us-east-1:581987831513:stack/Nessus-Thomas-101-2/41d38410-5729-11ed-a52f-127af16651f7'}, {'Key': 'aws:cloudformation:stack-id', 'Value': 'arn:aws:cloudformation:us-east-1:581987831513:stack/Nessus-Thomas-101-2/41d38410-5729-11ed-a52f-127af16651f7'}, **{'Key': 'NessusScanner', 'Value': 'TRUE'},** {'Key': 'aws:cloudformation:logical-id', 'Value': 'NessusEC2Instance'}, {'Key': 'aws:cloudformation:stack-name', 'Value': 'Nessus-Thomas-101-2'}]

我的主要兴趣是获得“{'Key': 'NessusScanner', 'Value': 'TRUE'},”

我似乎无法提取此内容或不确定如何提取。我不确定我是否需要使用线扫描仪或其他东西。

我正在尝试重新启动我的所有 Nessus 扫描仪,并使用键和值 Nessus 扫描仪 True。

请注意,我正在使用

describe_instances()

我尝试创建另一个 for 循环,但不断收到错误消息。

【问题讨论】:

    标签: python-3.x dictionary line


    【解决方案1】:

    找到答案或解决方法:

    def restart_lab_server(aws_client):
    
        response = aws_client.describe_instances()
        ###print(response)
        for reservation in response["Reservations"]:
            for instance in reservation["Instances"]:
                try:
                    x = instance["Tags"]
                    for y in x:
                        print(y["Value"])
    
                except:
                    print("This instance does not have any tags. Next Entry.")
    
    
        return 0

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-12
      • 1970-01-01
      • 2020-05-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多