【问题标题】:Break a list of dictionaries (json)打破字典列表(json)
【发布时间】:2020-06-21 20:49:07
【问题描述】:

我有一个以下 json data

对象'parents'的结构是:

parents:[{
           "efficiency_d": null,
           "efficiency_c": null, 
           "efficiency_a": null, 
           "color": null,  
           "internal_number_uek": null, 
           "name_uek": "Tixaquir", 
           "race": "LUSITANO", 
           "parents": [{
                        ...same as above ...,
                        "parents" : [{},{}]
                       },
                       {
                        ...same as above ...,
                        "parents" : [{},{]]
                       }]

         },
         {
           "efficiency_d": null,
           "efficiency_c": null, 
           "efficiency_a": null, 
           "color": null,  
           "internal_number_uek": null, 
           "name_uek": "Tixaquir", 
           "race": "LUSITANO", 
           "parents": [{},{}]
         }]

我想将每个列表“父母”分解为对象

sire = js['parents'][0]
dam = js['parents'][1]

但对于项目“父母”的所有级别(缩进)。 所以对象看起来像:

'sire' : {
           "efficiency_d": null,
           "efficiency_c": null, 
           "efficiency_a": null, 
           "color": null,  
           "internal_number_uek": null, 
           "name_uek": "Tixaquir", 
           "race": "LUSITANO", 
           "sire":{
                   ...same as above ...,
                   "sire" :{},
                   "dam"  :{}
                   },
            "dam" :{
                   ...same as above...,
                   "sire" :{},
                   "dam"  :{}
                    }

         },
'dam':  {
           "efficiency_d": null,
           "efficiency_c": null, 
           "efficiency_a": null, 
           "color": null,  
           "internal_number_uek": null, 
           "name_uek": "Tixaquir", 
           "race": "LUSITANO", 
           "sire": {},
           "dam" : {}
         }

我正在努力解决的问题是连锁项目“父母”。我尝试对从 json.loads(js.decode("utf-8")) 返回的 python 字典进行一些迭代,但总是失败。 我正在使用python 3。 提前致谢!

【问题讨论】:

    标签: json python-3.x dictionary


    【解决方案1】:

    希望我能正确理解您的问题:您想将每个 'parents':[...] 替换为 {'sire':..., 'dam':...} 结构。

    import json
    
    
    txt = '''{"efficiency_d": "T", "efficiency_c": null, "efficiency_a": null, "color": "Ryz.", "descendants_top_efficiency": {"a": null, "s": null, "c": null, "d": null}, "descendants": [{"efficiency_d": null, "efficiency_c": null, "efficiency_a": null, "color": "Ryz.", "internal_number_uek": null, "name_uek": "Nordico D' Pardinhos", "race": "LUSITANO", "efficiency_s": null, "licence": null, "id": "cd93f4a4-e48b-4bcc-a8bb-a9901f11f2cf", "birth_year": 2017}, {"efficiency_d": null, "efficiency_c": null, "efficiency_a": null, "color": "B\u011bl.", "internal_number_uek": null, "name_uek": "Lidador d'Pardinhos", "race": "LUSITANO", "efficiency_s": null, "licence": null, "id": "b1b24446-7d6d-486c-ab46-bff6d77ed1dc", "birth_year": 2015}, {"efficiency_d": null, "efficiency_c": null, "efficiency_a": null, "color": "B\u011bl.", "internal_number_uek": null, "name_uek": "Jade Dos Pardinhos", "race": "LUSITANO", "efficiency_s": null, "licence": null, "id": "a1d1ff36-8666-4fbf-8cd6-79eb427eb2c7", "birth_year": 2014}, {"efficiency_d": null, "efficiency_c": null, "efficiency_a": null, "color": "Ryz.", "internal_number_uek": null, "name_uek": "Jandaia D'Pardinhos", "race": "LUSITANO", "efficiency_s": null, "licence": null, "id": "4c19ebb2-6270-4a24-b322-ec035ae136cd", "birth_year": 2014}], "internal_number_uek": null, "name_uek": "Destino Pardinhos", "race": "LUSITANO", "parents": [{"efficiency_d": null, "efficiency_c": null, "efficiency_a": null, "color": null, "internal_number_uek": null, "name_uek": "Tixaquir", "race": "LUSITANO", "parents": [{"efficiency_d": null, "efficiency_c": null, "efficiency_a": null, "color": null, "internal_number_uek": null, "name_uek": "Xaquiro", "race": null, "parents": [{"parents": [{"parents": [{}, {}]}, {"parents": [{}, {}]}]}, {"parents": [{"parents": [{}, {}]}, {"parents": [{}, {}]}]}], "efficiency_s": null, "licence": null, "id": null, "birth_year": null}, {"efficiency_d": null, "efficiency_c": null, "efficiency_a": null, "color": null, "internal_number_uek": null, "name_uek": "Oxigenada", "race": null, "parents": [{"parents": [{"parents": [{}, {}]}, {"parents": [{}, {}]}]}, {"parents": [{"parents": [{}, {}]}, {"parents": [{}, {}]}]}], "efficiency_s": null, "licence": null, "id": null, "birth_year": null}], "efficiency_s": null, "licence": null, "id": "f62937b0-1af5-49aa-b65e-ec1b2849e529", "birth_year": 2000}, {"efficiency_d": null, "efficiency_c": null, "efficiency_a": null, "color": null, "internal_number_uek": null, "name_uek": "Unanime", "race": "LUSITANO", "parents": [{"efficiency_d": null, "efficiency_c": null, "efficiency_a": null, "color": null, "internal_number_uek": null, "name_uek": "Navegante", "race": null, "parents": [{"parents": [{"parents": [{}, {}]}, {"parents": [{}, {}]}]}, {"parents": [{"parents": [{}, {}]}, {"parents": [{}, {}]}]}], "efficiency_s": null, "licence": null, "id": null, "birth_year": null}, {"efficiency_d": null, "efficiency_c": null, "efficiency_a": null, "color": null, "internal_number_uek": null, "name_uek": "Piza", "race": null, "parents": [{"parents": [{"parents": [{}, {}]}, {"parents": [{}, {}]}]}, {"parents": [{"parents": [{}, {}]}, {"parents": [{}, {}]}]}], "efficiency_s": null, "licence": null, "id": null, "birth_year": null}], "efficiency_s": null, "licence": null, "id": "b13858af-2b60-4319-bd38-928912d6871a", "birth_year": 2001}], "efficiency_s": null, "licence": "KJ00TU", "id": "0c827a0a-9489-4580-a373-d20c5e27859f", "birth_year": 2008}'''
    data = json.loads(txt)
    
    def traverse(d):
        if 'parents' in d:
            sire, dam = d['parents']
            del d['parents']
            if sire:
                d['sire'] = sire
                traverse(sire)
            if dam:
                d['dam'] = dam
                traverse(dam)
    
    traverse(data)
    print(json.dumps(data, indent=4))
    

    打印:

    {
        "efficiency_d": "T",
        "efficiency_c": null,
        "efficiency_a": null,
        "color": "Ryz.",
        "descendants_top_efficiency": {
            "a": null,
            "s": null,
            "c": null,
            "d": null
        },
        "descendants": [
            {
                "efficiency_d": null,
                "efficiency_c": null,
                "efficiency_a": null,
                "color": "Ryz.",
                "internal_number_uek": null,
                "name_uek": "Nordico D' Pardinhos",
                "race": "LUSITANO",
                "efficiency_s": null,
                "licence": null,
                "id": "cd93f4a4-e48b-4bcc-a8bb-a9901f11f2cf",
                "birth_year": 2017
            },
            {
                "efficiency_d": null,
                "efficiency_c": null,
                "efficiency_a": null,
                "color": "B\u011bl.",
                "internal_number_uek": null,
                "name_uek": "Lidador d'Pardinhos",
                "race": "LUSITANO",
                "efficiency_s": null,
                "licence": null,
                "id": "b1b24446-7d6d-486c-ab46-bff6d77ed1dc",
                "birth_year": 2015
            },
            {
                "efficiency_d": null,
                "efficiency_c": null,
                "efficiency_a": null,
                "color": "B\u011bl.",
                "internal_number_uek": null,
                "name_uek": "Jade Dos Pardinhos",
                "race": "LUSITANO",
                "efficiency_s": null,
                "licence": null,
                "id": "a1d1ff36-8666-4fbf-8cd6-79eb427eb2c7",
                "birth_year": 2014
            },
            {
                "efficiency_d": null,
                "efficiency_c": null,
                "efficiency_a": null,
                "color": "Ryz.",
                "internal_number_uek": null,
                "name_uek": "Jandaia D'Pardinhos",
                "race": "LUSITANO",
                "efficiency_s": null,
                "licence": null,
                "id": "4c19ebb2-6270-4a24-b322-ec035ae136cd",
                "birth_year": 2014
            }
        ],
        "internal_number_uek": null,
        "name_uek": "Destino Pardinhos",
        "race": "LUSITANO",
        "efficiency_s": null,
        "licence": "KJ00TU",
        "id": "0c827a0a-9489-4580-a373-d20c5e27859f",
        "birth_year": 2008,
        "sire": {
            "efficiency_d": null,
            "efficiency_c": null,
            "efficiency_a": null,
            "color": null,
            "internal_number_uek": null,
            "name_uek": "Tixaquir",
            "race": "LUSITANO",
            "efficiency_s": null,
            "licence": null,
            "id": "f62937b0-1af5-49aa-b65e-ec1b2849e529",
            "birth_year": 2000,
            "sire": {
                "efficiency_d": null,
                "efficiency_c": null,
                "efficiency_a": null,
                "color": null,
                "internal_number_uek": null,
                "name_uek": "Xaquiro",
                "race": null,
                "efficiency_s": null,
                "licence": null,
                "id": null,
                "birth_year": null,
                "sire": {
                    "sire": {},
                    "dam": {}
                },
                "dam": {
                    "sire": {},
                    "dam": {}
                }
            },
            "dam": {
                "efficiency_d": null,
                "efficiency_c": null,
                "efficiency_a": null,
                "color": null,
                "internal_number_uek": null,
                "name_uek": "Oxigenada",
                "race": null,
                "efficiency_s": null,
                "licence": null,
                "id": null,
                "birth_year": null,
                "sire": {
                    "sire": {},
                    "dam": {}
                },
                "dam": {
                    "sire": {},
                    "dam": {}
                }
            }
        },
        "dam": {
            "efficiency_d": null,
            "efficiency_c": null,
            "efficiency_a": null,
            "color": null,
            "internal_number_uek": null,
            "name_uek": "Unanime",
            "race": "LUSITANO",
            "efficiency_s": null,
            "licence": null,
            "id": "b13858af-2b60-4319-bd38-928912d6871a",
            "birth_year": 2001,
            "sire": {
                "efficiency_d": null,
                "efficiency_c": null,
                "efficiency_a": null,
                "color": null,
                "internal_number_uek": null,
                "name_uek": "Navegante",
                "race": null,
                "efficiency_s": null,
                "licence": null,
                "id": null,
                "birth_year": null,
                "sire": {
                    "sire": {},
                    "dam": {}
                },
                "dam": {
                    "sire": {},
                    "dam": {}
                }
            },
            "dam": {
                "efficiency_d": null,
                "efficiency_c": null,
                "efficiency_a": null,
                "color": null,
                "internal_number_uek": null,
                "name_uek": "Piza",
                "race": null,
                "efficiency_s": null,
                "licence": null,
                "id": null,
                "birth_year": null,
                "sire": {
                    "sire": {},
                    "dam": {}
                },
                "dam": {
                    "sire": {},
                    "dam": {}
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-03-29
      • 1970-01-01
      • 2021-01-05
      • 2016-10-22
      • 2015-10-03
      • 1970-01-01
      • 2015-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多