在Python中将json或字典数据格式化为部分扁平结构

问题描述 投票:0回答:1
sample_data = {
  "instances": [
    {
      "a": "1987",
      "b": "2001",
      "art": {
        "009": {
          "art_id": "009",
          "mm": {
            "1002": {
              "mm_code": "1002",
              "node": {
                "6625583": {
                  "node_id": "6625583",
                  "node_sc": "186",
                  "node_si": [
                    {
                      "measure": "S",
                      "chest": 29
                    },
                    {
                      "measure": "M",
                      "chest": 32
                    }
                  ]
                }
              }
            }
          }
        }
      }
    }
  ]
}

我们需要什么? 我有样本数据,我需要将

sample["instances"][0]
转换为以下格式

sample_data = {
  "instances": [
    {
      "a": "1987",
      "b": "2001",
      "art_id": "009",
      "mm_code": "1002",
      "node_id": "6625583",
      "node_sc": "186",
      "measure": "S",
      "chest": 29
    },
    { 
      "a": "1987",
      "b": "2001",
      "art_id": "009",
      "mm_code": "1002",
      "node_id": "6625583",
      "node_sc": "186",
      "measure": "M",
      "chest": 32
    }
  ]
}

要求

  1. 需要一个非常耗时的解决方案
  2. 多个“art_id”或“art”和“mm”嵌套值也是可能的

尝试过这个解决方案: imran的解决方案但无法达到所需的格式。 感谢您提供的所有帮助

python-3.x list dictionary format flatten
1个回答
0
投票

这里是如何扁平化这本字典的可能解决方案:

sample_data = {
    "instances": [
        {
            "a": "1987",
            "b": "2001",
            "art": {
                "009": {
                    "art_id": "009",
                    "mm": {
                        "1002": {
                            "mm_code": "1002",
                            "node": {
                                "6625583": {
                                    "node_id": "6625583",
                                    "node_sc": "186",
                                    "node_si": [
                                        {"measure": "S", "chest": 29},
                                        {"measure": "M", "chest": 32},
                                    ],
                                }
                            },
                        }
                    },
                }
            },
        }
    ]
}


def find_node_si(dct):
    if "node_si" not in dct:
        next_dct = next(v for v in dct.values() if isinstance(v, dict))
        return find_node_si(next_dct)
    else:
        return dct["node_si"]


def return_all_key_values(dct):
    keys = [(k, v) for k, v in dct.items() if not isinstance(v, (list, dict))]
    next_dct = next((v for v in dct.values() if isinstance(v, dict)), None)
    if next_dct:
        return keys + return_all_key_values(next_dct)
    else:
        return keys


out = {"instances": []}
for i in sample_data["instances"]:
    dct = return_all_key_values(i)
    for node_si in find_node_si(i):
        out["instances"].append(dict(dct + list(node_si.items())))

print(out)

打印:

{
    "instances": [
        {
            "a": "1987",
            "b": "2001",
            "art_id": "009",
            "mm_code": "1002",
            "node_id": "6625583",
            "node_sc": "186",
            "measure": "S",
            "chest": 29,
        },
        {
            "a": "1987",
            "b": "2001",
            "art_id": "009",
            "mm_code": "1002",
            "node_id": "6625583",
            "node_sc": "186",
            "measure": "M",
            "chest": 32,
        },
    ]
}
© www.soinside.com 2019 - 2024. All rights reserved.