我有一个需要剪切的大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 & 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))
2条答案
按热度按时间ltskdhd11#
试试看:
图纸:
要保存回Json:
eulz3vhy2#
如果你只是想从列表中的每个字典中删除一些键,让我们试试这个