基于键Python从JSON中删除项目

k2fxgqgv  于 2022-12-15  发布在  Python
关注(0)|答案(2)|浏览(116)

我有一个需要剪切的大JSON文件,我正在尝试删除以下项目:“所有者”、“自动收报机”、“注解”和“ptr_link”作为关键字。
JSON文件:

{
  "transactions": {
        "0": [
            {
                "transaction_date": "11/29/2022",
                "owner": "Spouse",
                "ticker": "<a href=\"https://finance.yahoo.com/q?s=WIW\" target=\"_blank\">WIW</a>",
                "asset_description": "Western Asset Inflation-Linked Opportunities &amp; Inc",
                "asset_type": "Stock",
                "type": "Sale (Full)",
                "amount": "$1,001 - $15,000",
                "comment": "--",
                "ptr_link": "https://efdsearch.senate.gov/search/view/ptr/5ac4d053-0258-4531-af39-8a8067f0d085/"
            },
            {
                "transaction_date": "11/29/2022",
                "owner": "Spouse",
                "ticker": "<a href=\"https://finance.yahoo.com/q?s=GBIL\" target=\"_blank\">GBIL</a>",
                "asset_description": "Goldman Sachs Access Treasury 0-1 Year ETF",
                "asset_type": "Other Securities",
                "type": "Purchase",
                "amount": "$1,001 - $15,000",
                "comment": "--",
                "ptr_link": "https://efdsearch.senate.gov/search/view/ptr/5ac4d053-0258-4531-af39-8a8067f0d085/"
            }
          ]
     }
}

保存这个列表的“0”的范围可以达到60,所以我需要集体访问所有的,而不是专门为这个列表写。同样的道理也适用于保存键/值的字典,因为可能有很多数量,所以我不能输入[0]或[1]等。
这是我的代码,我试图过滤到相应的对象,并简单地删除键。虽然我需要这样做的集体提到。

import json

data = json.load(open("xxxtester.json"))

data1 = data['transactions']
data2 = data1['0'][0]

for i in data2:
    del data2['owner']
for i in data2:
    del data2['ticker']
for i in data2:
    del data2['comment']
for i in data2:
    del data2['ptr_link']

open("xxxtester.json", "w").write(json.dumps(data, indent=4))
ltskdhd1

ltskdhd11#

试试看:

import json

with open("your_data.json", "r") as f_in:
    data = json.load(f_in)

to_delete = {"owner", "ticker", "comment", "ptr_link"}

for k in data["transactions"]:
    data["transactions"][k] = [
        {kk: vv for kk, vv in d.items() if kk not in to_delete}
        for d in data["transactions"][k]
    ]

print(data)

图纸:

{
    "transactions": {
        "0": [
            {
                "transaction_date": "11/29/2022",
                "asset_description": "Western Asset Inflation-Linked Opportunities &amp; Inc",
                "asset_type": "Stock",
                "type": "Sale (Full)",
                "amount": "$1,001 - $15,000",
            },
            {
                "transaction_date": "11/29/2022",
                "asset_description": "Goldman Sachs Access Treasury 0-1 Year ETF",
                "asset_type": "Other Securities",
                "type": "Purchase",
                "amount": "$1,001 - $15,000",
            },
        ]
    }
}

要保存回Json:

with open("output.json", "w") as f_out:
    json.dump(data, f_out, indent=4)
eulz3vhy

eulz3vhy2#

如果你只是想从列表中的每个字典中删除一些键,让我们试试这个

data = json.load(open("xxxtester.json"))
for_delete = ["owner", "ticker", "comment", "ptr_link"]

for d in data['transactions']['0']:
    for key in for_delete:
        if key in d:
            d.pop(key)

open("xxxtester.json", "w").write(
    json.dumps(data, indent=4))

相关问题