簡體   English   中英

Python從包含列表的嵌套json中刪除空字典

[英]Python remove empty dictionary from nested json containing list

我有一個深層嵌套的 json 文件,如下所示:

dict = [
{
"date":"2017-05-31",
"sections":[
 {
    "item":"BalanceSheetFormat2Heading",
    "value":"None",
    "sections":[
       {
          "item":"TotalAssets",
          "value":"None",
          "sections":[
             {
                "item":"FixedAssets",
                "value":"None",
                "sections":[
                   {
                      "item":"IntangibleAssets",
                      "value":"None",
                      "sections":[

                      ]
                   },
                   {
                      "item":"PropertyPlantEquipment",
                      "value":"None",
                      "sections":[

                      ]
                   },
                   {
                      "item":"InvestmentsFixedAssets",
                      "value":"None",
                      "sections":[
                         {
                            "item":"LoansToGroupUndertakings",
                            "value":"None",
                            "sections":[

                            ]
                         },
                         {
                            "item":"OwnShares",
                            "value":"None",
                            "sections":[

                            ]
                         }
                      ]
                   },
                   {
                      "item":"InvestmentProperty",
                      "value":"None",
                      "sections":[

                      ]
                   },
                   {
                      "item":"BiologicalAssetsNon-current",
                      "value":"None",
                      "sections":[

                      ]
                   }
                ]
             },
             {
                "item":"CurrentAssets",
                "value":"None",
                "sections":[
                   {
                      "item":"TotalInventories",
                      "value":"None",
                      "sections":[

                      ]
                   },
                   {
                      "item":"BiologicalAssetsCurrent",
                      "value":"None",
                      "sections":[

                      ]
                   },
                   {
                      "item":"Debtors",
                      "value":"None",
                      "sections":[
                         {
                            "item":"PrepaymentsAccruedIncome",
                            "value":"None",
                            "sections":[

                            ]
                         },
                         {
                            "item":"DeferredTaxAssetDebtors",
                            "value":"None",
                            "sections":[

                            ]
                         }
                      ]
                   },
                   {
                      "item":"CurrentAssetInvestments",
                      "value":"None",
                      "sections":[
                         {
                            "item":"InvestmentsInGroupUndertakings",
                            "value":"None",
                            "sections":[

                            ]
                         },
                         {
                            "item":"OwnShares",
                            "value":"None",
                            "sections":[

                            ]
                         }
                      ]
                   },
                   {
                      "item":"CashBankOnHand",
                      "value":"None",
                      "sections":[

                      ]
                   }
                ]
             },
             {
                "item":"PrepaymentsAccruedIncome",
                "value":"None",
                "sections":[

                ]
             }
          ]
       },
       {
          "item":"TotalLiabilities",
          "value":"None",
          "sections":[
             {
                "item":"Equity",
                "value":9014904.0,
                "sections":[

                ]
             },
             {
                "item":"ProvisionsFor",
                "value":"None",
                "sections":[
                   {
                      "item":"RetirementBenefitObligationsSurplus",
                      "value":"None",
                      "sections":[

                      ]
                   }
                ]
             },
             {
                "item":"Creditors",
                "value":"None",
                "sections":[
                   {
                      "item":"UseCurrentNon",
                      "value":"None",
                      "sections":[

                      ]
                   },
                   {
                      "item":"TradeCreditorsTradePayables",
                      "value":"None",
                      "sections":[

                      ]
                   }
                ]
             },
             {
                "item":"AccruedLiabilitiesNot",
                "value":"None",
                "sections":[

                ]
             }
          ]
       }
    ]
 }
]
}
]

我想要實現的是刪除具有空sectionsvalue等於None的對象,例如應該從字典中刪除整個對象

{
    "item":"IntangibleAssets",
     "value":"None",
      "sections":[]
                  
 }

最終輸出應如下所示:

[
  {
    "date":"2017-05-31",
    "sections":[
  {
    "item":"BalanceSheetFormat2Heading",
    "value":"None",
    "sections":[
       {
          "item":"TotalLiabilities",
          "value":"None",
          "sections":[
             {
                "item":"Equity",
                "value":9014904.0,
                "sections":[

                ]
             }
          ]
       }
    ]
 }
]
}
]

我曾嘗試使用此函數檢查對象是否為空:

def is_single_element(obj):
    # print(obj)
    if isinstance(obj, dict):
        if "item" in obj and "value" in obj and "sections" in obj:
            if obj["value"] == "None" and len(obj["sections"]) == 0:
                return True
    return False

並遞歸地遍歷 json 並使用以下方法刪除那些 obj:

def remove_single_obj(dict_):
    if isinstance(dict_, dict):
        for k, v in list(dict_.items()):
            if is_single_element(v):
                remove_single_obj(v)
    if isinstance(dict_, list):
        for index in range(len(dict_)):
            if is_single_element(dict_[index]):
                dict_.pop(index)
        remove_single_obj(dict_)
    return dict_

但我仍然無法獲得所需的結果。 非常感謝任何幫助

首先,這看起來很可疑:

for index in range(len(dict_)):
    if is_single_element(dict_[index]):
        dict_.pop(index)

請注意,這里的dict_不是一個 dict,而是一個列表......然后,假設您有一個包含 3 個元素的列表,[A, B, C],其中前兩個應該被刪除。 首先,您將刪除項目 A,使列表 [B, C]。 然后循環從索引 1 開始,因此從未查看過項目 B! 最后,它查看超出范圍的項目 2!

這是一個工作代碼。
它假設數據根是一個列表,並且列表項是字典,並且所有字典都遞歸地具有“部分”列表。 所以不需要通過isinstance檢查類型。

def delete_emtpy_from_l(l):
    len0 = len(l)
    l[:] = [d for d in l if 'value' in d and d['value'] != 'None' or d['sections']]
    cnt = len0 - len(l)
    for d in l:
        cnt += delete_emtpy_from_l(d['sections'])
    # cnt is how many dict are deleted
    return cnt


# loop until no new dict is deleted
while delete_emtpy_from_l(data):
    pass

pprint(data)

輸出:

[{'date': '2017-05-31',
  'sections': [{'item': 'BalanceSheetFormat2Heading',
                'sections': [{'item': 'TotalLiabilities',
                              'sections': [{'item': 'Equity',
                                            'sections': [],
                                            'value': 9014904.0}],
                              'value': 'None'}],
                'value': 'None'}]}]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM