2017-11-01 10 views
-2

リストにあるjsonデータを割り振りに基づいて削除したいのですが、の割り当て:Trueの場合、その特定の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をありがとう

+0

が重複する可能性の:https://stackoverflow.com/questions/19167485/removing-json-property-in-array-of-objects -with-python とhttps://stackoverflow.com/questions/36606930/delete-an-element-in-a-json-object とhttps://stackoverflow.com/questions/192/how-to-delete -json-object-using-python – DRPK

+0

[Pythonでオブジェクトの配列内のJSONプロパティを削除する]の可能な複製(https://stackoverflow.com/questions/19167485/removing-json-property-in-array-of-objects-パイソンと一緒に) – DRPK

答えて

1

私は削除する事は、プリンタ全体または単にモデルがあるかどうかわからないんだけど、これは前者の場合のために働くとにシンプルであるべきです後者に変換(continueをダンプし、modeld['allPrinters']を置き換える):

for i, printer in enumerate(d['allPrinters']): 
    for model in printer['models']: 
     if model['allocation']: 
      d['allPrinters'].pop(i) 
      continue 
関連する問題