簡體   English   中英

根據Python中的布爾值從字典中刪除元數據

[英]Remove meta data from dictionary Based on Boolean value from Python

我想根據分配從列表中刪除 json 數據。如果分配:那么,那個特定的 josn 應該從字典中刪除。 在下面的數據{"paperWidth": "4","allocation": True,"model": "RB3150","connType": ["Lan","Wifi","BlueTooth"],"commandSet": " TVSPOS"}

所以這應該從我的最終對象中刪除。如何在 python 中做到這一點。

d = {"allPrinters": [{"models": [{"paperWidth": "4","allocation": True,"model": "RB3150","connType": ["Lan","Wifi","BlueTooth"],"commandSet": "TVSPOS"},{"paperWidth": "4","allocation": False,"model": "RB3151","connType": ["Lan","Wifi"],"commandSet": "TVSPOS"}],"active": True,"make": "TVS","_id": "59e6f670f532eb1260b50cd9"},{"models": [{"paperWidth": "4","allocation": False,"model": "EP3160","connType": ["Lan","Wifi"],"commandSet": "ESCPOS"},{"paperWidth": "4","allocation": True,"model": "EP3161","connType": ["Lan"],"commandSet": "ESCPOS"}],"active": True,"make": "EPSON","_id": "59e6fc8ff532eb1260b50cf4"}]}

謝謝你,拉梅什 M

我不確定您要刪除的內容是整個打印機還是模型,但這應該適用於前一種情況,並且很容易轉換為后者(轉儲continue並將d['allPrinters']替換為model ):

for i, printer in enumerate(d['allPrinters']):
     for model in printer['models']:
         if model['allocation']:
             d['allPrinters'].pop(i)
             continue

暫無
暫無

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

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