简体   繁体   English

Python 3.8 - 无法更新词典

[英]Python 3.8 - Unable to update dictionary

Hello I have this below dictionary that I want to update您好,我有下面这本字典,我想更新

{
   "Version":"2012-10-17",
   "Statement":[
      {
         "Effect":"Allow",
         "Principal":{
            "Federated":"arn:aws:iam::111111111111:saml-provider/Test"
         },
         "Action":"sts:AssumeRoleWithSAML",
         "Condition":{
            "StringEquals":{
               "SAML:aud":"https://signin.aws.amazon.com/saml"
            }
         }
      }
   ]
}

Want to update it to想更新到

{
   "Version":"2012-10-17",
   "Statement":[
      {
         "Effect":"Allow",
         "Principal":{
            "Federated":[
            "arn:aws:iam::111111111111:saml-provider/Test", 
            "arn:aws:iam::111111111111:saml-provider/Test2"
            ]
         },
         "Action":"sts:AssumeRoleWithSAML",
         "Condition":{
            "StringEquals":{
               "SAML:aud":"https://signin.aws.amazon.com/saml"
            }
         }
      }
   ]
}

ie add "arn:aws:iam::111111111111:saml-provider/Test2" to "Federated" and also make it a list.即将“arn:aws:iam::111111111111:saml-provider/Test2”添加到“Federated”并使其成为一个列表。 Below is my code下面是我的代码

    new_arn = "arn:aws:iam::111111111111:saml-provider/Test2"
    my_dict = {
   "Version":"2012-10-17",
   "Statement":[
      {
             "Effect":"Allow",
             "Principal":{
                "Federated":[
                "arn:aws:iam::111111111111:saml-provider/Test",
                ]
             },
             "Action":"sts:AssumeRoleWithSAML",
             "Condition":{
                "StringEquals":{
                   "SAML:aud":"https://signin.aws.amazon.com/saml"
                }
             }
          }
       ]
    }
    for b in my_dict['Statement']:
        updated_arn = f"['{b['Principal']['Federated']}', {new_arn}]"
        b['Principal']['Federated']: updated_arn

    print(my_dict)

I am bit new to python and I am not sure what am I doing wrong the dict is not getting updated.我对 python 有点陌生,我不确定我做错了什么字典没有更新。 Can someone please provide some guidance on what I may be doing wrong有人可以就我可能做错的事情提供一些指导吗

As folks commented, you're constructing a string that looks like a list here:正如人们评论的那样,您正在构建一个看起来像列表的字符串:

    for b in my_dict['Statement']:
        updated_arn = f"['{b['Principal']['Federated']}', {new_arn}]"
        b['Principal']['Federated']: updated_arn

You can create a real list instead:您可以改为创建一个真实列表:

    for b in my_dict['Statement']:
        updated_arn = [b['Principal']['Federated'], new_arn]
        b['Principal']['Federated'] = updated_arn
        # note it's `=` not `:` here

Edit: If Federated is sometimes a string, and sometimes already a list, you'll need to check its type and act accordingly:编辑:如果Federated有时是一个字符串,有时已经是一个列表,您需要检查它的类型并采取相应的行动:

    for b in my_dict['Statement']:
        federated = b['Principal']['Federated']
        if isinstance(federated, list):
            federated.append(new_arn)
            # (which updates the list within the dict, no need to assign back to the dict)
        else:
            b['Principal']['Federated'] = [federated, new_arn]

You can append to the current value of "Federated" as follows:您可以将 append 设为“Federated”的当前值,如下所示:

for b in my_dict['Statement']:
       b['Principal']['Federated'].append(new_arn)

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

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