简体   繁体   English

循环通过字典 python 的问题

[英]Issues looping through dict python

I'm running into an issue with actually successfully looping through a boto3 result for AWS Config.我在实际成功循环 AWS Config 的 boto3 结果时遇到了问题。 From my testing, I am able to perform the loop;根据我的测试,我能够执行循环; however, it only outputs same violation instead of each violation for the different ARNs.但是,它只输出相同的违规而不是针对不同 ARN 的每个违规。

I realize where the issue is at in the code as I was doing the below:当我执行以下操作时,我意识到代码中的问题出在哪里:

for arn in configRule.items():
          acmArn = configRule['EvaluationResults'][0]['EvaluationResultIdentifier']['EvaluationResultQualifier']['ResourceId']

But, when I try and take out the [0] so it is not a specific item I get "list indices must be integers or slices, not str".但是,当我尝试取出[0]所以它不是特定项目时,我得到“列表索引必须是整数或切片,而不是 str”。

My question is if there is an actual way to get this value to be incremented, so it will be able to get the different ARNs of the violations?我的问题是,是否有一种实际的方法来增加这个值,所以它能够获得不同的违规 ARN? Sorry if this is an easy answer, still new to Python, so trying to figure all this out.抱歉,如果这是一个简单的答案,对于 Python 来说仍然是新的,所以试图弄清楚这一切。

Output Example: Output 示例:

{
    "EvaluationResults": [
      {
        "Annotation": "Certificate will expire on 2023-01-21T23:59:59.000Z",
        "ComplianceType": "NON_COMPLIANT",
        "ConfigRuleInvokedTime": "2022-01-03 18:28:54.939000+00:00",
        "EvaluationResultIdentifier": {
          "EvaluationResultQualifier": {
            "ConfigRuleName": "acm-certificate-expiration-check",
            "ResourceId": "arn:aws:acm:us-west-2:xxxx:certificate/39a95537-e5aa-46dd-bc9b-04d7b2606bd0",
            "ResourceType": "AWS::ACM::Certificate"
          },
          "OrderingTimestamp": "2021-12-22 00:29:01+00:00"
        },
        "ResultRecordedTime": "2022-01-03 18:28:55.672000+00:00"
      },
      {
        "Annotation": "Certificate will expire on 2023-01-10T23:59:59.000Z",
        "ComplianceType": "NON_COMPLIANT",
        "ConfigRuleInvokedTime": "2022-01-03 18:28:54.939000+00:00",
        "EvaluationResultIdentifier": {
          "EvaluationResultQualifier": {
            "ConfigRuleName": "acm-certificate-expiration-check",
            "ResourceId": "arn:aws:acm:us-west-2:xxxx:certificate/493de1e8-2bcb-42c7-96df-ce88bdeac64c",
            "ResourceType": "AWS::ACM::Certificate"
          },
          "OrderingTimestamp": "2021-12-12 18:25:14+00:00"
        },
        "ResultRecordedTime": "2022-01-03 18:28:55.683000+00:00"
      }
    ],
    "ResponseMetadata": {
      "HTTPHeaders": {
        "content-length": "955",
        "content-type": "application/x-amz-json-1.1",
        "date": "Mon, 03 Jan 2022 20:13:06 GMT",
        "strict-transport-security": "max-age=86400",
        "x-amzn-requestid": "a6e51323-9e4c-44c7-a15a-ea0314392ab6"
      },
      "HTTPStatusCode": 200,
      "RequestId": "a6e51323-9e4c-44c7-a15a-ea0314392ab6",
      "RetryAttempts": 0
    }
  }

So, I'm not sure if this is the correct way to do this, but for now this is how I got this to work.所以,我不确定这是否是正确的方法,但现在这就是我让它工作的方式。 This allows me to add a variable there and increase it each time it performs the loop.这允许我在那里添加一个变量并在每次执行循环时增加它。

I'm sure there is a better way to do this, but just what I found doing some testing.我确信有更好的方法可以做到这一点,但只是我发现做一些测试。

    i = 0
    for key, value in configRule.items():
        acmArn = configRule['EvaluationResults'][i]['EvaluationResultIdentifier']['EvaluationResultQualifier']['ResourceId']

        i+=1

configRules.items() for your example would be a sequence containing a single tuple, ('EvaluationResults', [...]) .您的示例的configRules.items()将是一个包含单个元组('EvaluationResults', [...])的序列。 You don't really care about that key: you hard-code it in the body.您并不真正关心该密钥:您将其硬编码在正文中。 What you really want is to iterate over the dict s in the list configRule['EvaluationResults'] :您真正想要的是迭代列表configRule['EvaluationResults']中的dict

for arn in configRule['EvaluationResults']:
    acmArn = arn['EvaluationResultIdentifier']['EvaluationResultQualifier']['ResourceId']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM