第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何過濾 json 并打印到新的 json 文件中?

如何過濾 json 并打印到新的 json 文件中?

慕哥6287543 2023-08-08 10:52:15
我有下面部分不完整的 python 代碼,我試圖用它來簡單地解析這個 JSON 對象并將結(jié)果寫入一個新的 json 文件,或者甚至將結(jié)果輸出到控制臺。我只想返回包含price_catof的節(jié)點,并且如果可能的話,'C'我還想從每個對象中完全刪除整個節(jié)點。'History'我做錯了什么以及我怎樣才能簡單地實現(xiàn)這一目標(biāo)?import json input_json = "" "   [       {           " type ": " 1 ",           " name ": " name 1 ",           "history":[             {               "expiration_date":"9999-12-31",               "effective_date":"2017-01-01"             }            ],            "prices":[             {               "price":"3.00",               "price_cat":"C",             }           ]       },       {           " type ": " 2 ",           " name ": " name 2 ",           "history":[             {               "expiration_date":"9999-12-31",               "effective_date":"2017-01-01"             }            ],            "prices":[             {               "price":"3.00",               "price_cat":"A",             },             {               "price":"3.00",               "price_cat":"C",             }           ]       },       {           " type ": " 1 ",           " name ": " name 3 ",           "history":[             {               "expiration_date":"9999-12-31",               "effective_date":"2017-01-01"             }            ],            "prices":[             {               "price":"3.00",               "price_cat":"B",             }           ]       }   ]" ""   #Transform json input to python objects     input_dict = json.loads (input_json)   #Filter python objects with list comprehensions     output_dict =[x for x in input_dict if x['price_cat'] == 'C']   #Transform python object back into json     output_json = json.dumps (output_dict)   #Show json       print (output_json)
查看完整描述

2 回答

?
郎朗坤

TA貢獻1921條經(jīng)驗 獲得超9個贊

您沒有在字典中查找價格表:


import json

input_json = """

[

    {

        " type ":" 1 ",

        " name ":" name 1 ",

        "history":[

             {

                "expiration_date":"9999-12-31",

                "effective_date":"2017-01-01"

             }

        ],

        "prices":[

             {

                "price":"3.00",

                "price_cat":"C"

             }]

        },

        {

        " type ":" 2 ",

        " name ":" name 2 ",

        "history":[

             {

                "expiration_date":"9999-12-31",

                "effective_date":"2017-01-01"

             }],

        "prices":[

             {

                "price":"3.00",

                "price_cat":"A"

             },

             {

                "price":"3.00",

                "price_cat":"C"

             }

        ]

        },

            {

            " type ":" 1 ",

            " name ":" name 3 ",

            "history":[

                 {

                    "expiration_date":"9999-12-31",

                    "effective_date":"2017-01-01"

                 }

            ],

            "prices":[

                 {

                    "price":"3.00",

                    "price_cat":"B"

                 }

            ]

    }

]"""


#Transform json input to python objects

input_dict = json.loads(input_json)

#Filter python objects with list comprehensions

output_dict = []

for input_subdict in input_dict:

    matching_prices = []

    for price in input_subdict['prices']:

        if price['price_cat'] == 'C':

            matching_prices.append(price)

    if len(matching_prices) > 0:

        input_subdict['prices'] = matching_prices

        output_dict.append(input_subdict)


#Transform python object back into json


output_json = json.dumps(output_dict)

#Show json

print (output_json)

這會產(chǎn)生您正在尋找的答案:


[

    {" type ": " 1 ", " name ": " name 1 ", "history": [{"expiration_date": "9999-12-31", "effective_date": "2017-01-01"}], "prices": [{"price": "3.00", "price_cat": "C"}]}, 

    {" type ": " 2 ", " name ": " name 2 ", "history": [{"expiration_date": "9999-12-31", "effective_date": "2017-01-01"}], "prices": [{"price": "3.00", "price_cat": "C"}]}

]


查看完整回答
反對 回復(fù) 2023-08-08
?
慕斯709654

TA貢獻1840條經(jīng)驗 獲得超5個贊

在嘗試查找價格類別之前,您似乎忘記將索引向下一級索引。這樣寫會很有幫助。


parseObjects = []

for jObject in input_json:

  for price in jObject["prices"]:

    if price["price_cat"] == "C":

      if "history" in jObject:

        del jObject["history"]

      parseObjects.append(jObject)

發(fā)生在我們最好的人身上:)。


查看完整回答
反對 回復(fù) 2023-08-08
  • 2 回答
  • 0 關(guān)注
  • 150 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號