根据动态键在字典中创建字典(包含数字),例如'abx.123':'xyz'。将与Dict相似的数字分组并制作单个dict

时间:2018-10-12 06:37:35

标签: python json

我有一个dict对象,其中有键和值对。所以我想在密钥的基础上添加密钥  例如

{'abx.123.a':'name',
'abz.123.b':'address',
aby.123.c':'location',
'abc.231.a':'Postion',
'abc.231.b':'dob'}

现在,我想将包含123数字的密钥添加到dict,并在dict中创建一个dict。并将包含231号的密钥添加到字典中。

Data = {
"arn": "arn",
"description": "Security group for all coporate communications",
"egress.#": "1",
"egress.4820.cidr_blocks.#": "1",
"egress.4820.cidr_blocks.0": "0.0.0.0/0",
"egress.4820.description": "",
"egress.4820.from_port": "0",
"egress.4820.ipv6_cidr_blocks.#": "0",
"egress.4820.prefix_list_ids.#": "0",
"egress.4820.protocol": "-1",
"egress.4820.security_groups.#": "0",
"egress.4820.self": "False",
"egress.4820.to_port": "0",
"id": "sg-080b03",
"ingress.#": "4",
"ingress.1279476397.cidr_blocks.#": "0",
"ingress.1279476397.description": "self",
"ingress.1279476397.from_port": "0",
"ingress.1279476397.prefix_list_ids.#": "0",
"ingress.1279476397.protocol": "-1",
"ingress.1279476397.security_groups.#": "0",
"ingress.1279476397.self": "true",
"ingress.1279476397.to_port": "0",
"ingress.2455438834.cidr_blocks.0": "10.10.0.0/16",
"ingress.2455438834.description": "cluster VPC",
"ingress.2455438834.from_port": "443",
"ingress.2455438834.protocol": "tcp",
"ingress.2455438834.to_port": "443",
"ingress.3391123749.cidr_blocks.#": "0",
"ingress.3391123749.description": "eks-cluster-master",
"ingress.3391123749.from_port": "443",
"ingress.3391123749.protocol": "tcp",
"ingress.3391123749.to_port": "443",
"ingress.439086653.cidr_blocks.#": "0",
"ingress.439086653.description": "eks-cluster-master",
"ingress.439086653.from_port": "1025",
"ingress.439086653.ipv6_cidr_blocks.#": "0",
"ingress.439086653.prefix_list_ids.#": "0",
"ingress.439086653.protocol": "tcp",
"ingress.439086653.security_groups.#": "1",
"ingress.439086653.security_groups.3696519931": "sg-0007a603523411",
"ingress.439086653.self": "False",
"ingress.439086653.to_port": "65535",
"name":"xyz.abc.corporate",
"owner_id": "12345678",
"revoke_rules_on_delete": "False",
"tags.%": "2",
"tags.Name": "abc.xyz.pqr",
"tags.abc": "owned"
}

创建一个在数据字典中具有相同编号的字典。 我要这样该数字将来会动态变化 例如

Data = [{
"arn": "arn",
"description": "Security group for all coporate communications",
"egress.#": "1",
{
  "egress.4820.cidr_blocks.#": "1",
  "egress.4820.cidr_blocks.0": "0.0.0.0/0",
  "egress.4820.description": "",
  "egress.4820.from_port": "0",
  "egress.4820.ipv6_cidr_blocks.#": "0",
  "egress.4820.prefix_list_ids.#": "0",
  "egress.4820.protocol": "-1",
  "egress.4820.security_groups.#": "0",
  "egress.4820.self": "False",
  "egress.4820.to_port": "0",
},
"id": "sg-080b03",
"ingress.#": "4",
{
  "ingress.1279476397.cidr_blocks.#": "0",
  "ingress.1279476397.description": "self",
  "ingress.1279476397.from_port": "0",
  "ingress.1279476397.prefix_list_ids.#": "0",
  "ingress.1279476397.protocol": "-1",
  "ingress.1279476397.security_groups.#": "0",
  "ingress.1279476397.self": "true",
  "ingress.1279476397.to_port": "0"
},
{
  "ingress.2455438834.cidr_blocks.0": "10.10.0.0/16",
  "ingress.2455438834.description": "cluster VPC",
  "ingress.2455438834.from_port": "443",
  "ingress.2455438834.protocol": "tcp",
  "ingress.2455438834.to_port": "443"
 },
 {
   "ingress.3391123749.cidr_blocks.#": "0",
   "ingress.3391123749.description": "eks-cluster-master",
   "ingress.3391123749.from_port": "443",
   "ingress.3391123749.protocol": "tcp",
   "ingress.3391123749.to_port": "443"
 },
 {
   "ingress.439086653.cidr_blocks.#": "0",
   "ingress.439086653.description": "eks-cluster-master",
   "ingress.439086653.from_port": "1025",
   "ingress.439086653.ipv6_cidr_blocks.#": "0",
   "ingress.439086653.prefix_list_ids.#": "0",
   "ingress.439086653.protocol": "tcp",
   "ingress.439086653.security_groups.#": "1",
   "ingress.439086653.security_groups.3631": "sg-0007",
   "ingress.439086653.self": "False",
   "ingress.439086653.to_port": "65535"
},
"name":"xyz.abc.corporate",
"owner_id": "12345678",
"revoke_rules_on_delete": "False",
"tags.%": "2",
"tags.Name": "abc.xyz.pqr",
"tags.abc": "owned"
}]

如果有人知道这个逻辑,您能建议我吗?

1 个答案:

答案 0 :(得分:0)

正如克里斯蒂安·克尼格(ChristianKönig)早已jqXHR object

  

您想要的结果不是有效的python数据结构。

因为您的代码包含以下内容:

{
  # ...
  "egress.#": "1", # <-- ok
  {                # <-- not ok (key is missing)
    "egress.4820.cidr_blocks.#": "1",
    "egress.4820.cidr_blocks.0": "0.0.0.0/0",
    "egress.4820.description": "",
    "egress.4820.from_port": "0",
    "egress.4820.ipv6_cidr_blocks.#": "0",
    "egress.4820.prefix_list_ids.#": "0",
    "egress.4820.protocol": "-1",
    "egress.4820.security_groups.#": "0",
    "egress.4820.self": "False",
    "egress.4820.to_port": "0",
  },
}

但是,如果您要像这样完全展开数据(可能效率不高):

Data: {
   "description": "Security group for all coporate communications"
   "arn": "arn"
   "name": "xyz.abc.corporate"
   "owner_id": "12345678"
   "tags": {
      "Name": "abc.xyz.pqr"
      "%": "2"
      "abc": "owned"
   }
   "revoke_rules_on_delete": "False"
   "egress": {
      "4820": {
         "description": ""
         "ipv6_cidr_blocks": {
            "#": "0"
         }
         "prefix_list_ids": {
            "#": "0"
         }
         "to_port": "0"
         "cidr_blocks": {
            "#": "1"
            "0": "0.0.0.0/0"
         }
         "security_groups": {
            "#": "0"
         }
         "self": "False"
         "protocol": "-1"
         "from_port": "0"
      }
      "#": "1"
   }
   "id": "sg-080b03"
   "ingress": {
      "1279476397": {
         "description": "self"
         "prefix_list_ids": {
            "#": "0"
         }
         "to_port": "0"
         "cidr_blocks": {
            "#": "0"
         }
         "security_groups": {
            "#": "0"
         }
         "self": "true"
         "protocol": "-1"
         "from_port": "0"
      }
      "3391123749": {
         "description": "eks-cluster-master"
         "cidr_blocks": {
            "#": "0"
         }
         "to_port": "443"
         "protocol": "tcp"
         "from_port": "443"
      }
      "439086653": {
         "description": "eks-cluster-master"
         "ipv6_cidr_blocks": {
            "#": "0"
         }
         "prefix_list_ids": {
            "#": "0"
         }
         "to_port": "65535"
         "cidr_blocks": {
            "#": "0"
         }
         "security_groups": {
            "3696519931": "sg-0007a603523411"
            "#": "1"
         }
         "self": "False"
         "protocol": "tcp"
         "from_port": "1025"
      }
      "2455438834": {
         "to_port": "443"
         "cidr_blocks": {
            "0": "10.10.0.0/16"
         }
         "protocol": "tcp"
         "description": "cluster VPC"
         "from_port": "443"
      }
      "#": "4"
   }
}

您可以使用以下代码:

def transformData(data, separator = '.'):
    def insertRecursive(dict_, key, value): # Recursive
        ''' inserts recursively:
            mydict = makeDefaultDict()
            insertRecursive(mydict, "deply.nested.value", 42)
            print(mydict["deply"]["nested"]["value"]) # prints 42 '''
        l, dot, r = key.partition(separator) # partition at first separator
        if dot == "": # if there was no separator left
            dict_[l] = value
        else:
            insertRecursive(dict_[l], r, value)

    def makeDefaultDict(): # Recursive
        ''' creates a dictionary thats default value is another dict with the same properties
            so you can do something like this:
            mydict = makeDefaultDict()
            mydict["deply"]["nested"]["value"] = 42 '''
        return defaultdict(makeDefaultDict) 

    # actual code:
    result = makeDefaultDict()
    for key, value in data.items(): # for each entry in data:
        insertRecursive(result, key, value)
    return result