[英]Remove/ filter rows in JSON based on condition with python
我在 JSON 中有一个表,如果“交易类型”中的“处置(非公开市场)”然后删除/过滤所有列中的条目,我需要根据条件删除/过滤整行。 下面是我的 JSON 文件的样子:
{
"lastDate":{
"0":"11\/22\/2022",
"1":"10\/28\/2022",
"2":"10\/17\/2022",
"3":"10\/15\/2022",
"4":"10\/15\/2022",
"5":"10\/15\/2022",
"6":"10\/15\/2022",
"7":"10\/03\/2022",
"8":"10\/03\/2022",
"9":"10\/03\/2022",
"10":"10\/01\/2022",
"11":"10\/01\/2022",
"12":"10\/01\/2022",
"13":"10\/01\/2022",
"14":"10\/01\/2022",
"15":"10\/01\/2022",
"16":"10\/01\/2022",
"17":"10\/01\/2022",
"18":"08\/17\/2022",
"19":"08\/08\/2022",
"20":"08\/05\/2022",
"21":"08\/05\/2022",
"22":"08\/03\/2022",
"23":"05\/06\/2022",
"24":"05\/04\/2022"
},
"transactionType":{
"0":"Sell",
"1":"Automatic Sell",
"2":"Automatic Sell",
"3":"Disposition (Non Open Market)",
"4":"Option Execute",
"5":"Disposition (Non Open Market)",
"6":"Option Execute",
"7":"Automatic Sell",
"8":"Sell",
"9":"Automatic Sell",
"10":"Disposition (Non Open Market)",
"11":"Option Execute",
"12":"Disposition (Non Open Market)",
"13":"Option Execute",
"14":"Disposition (Non Open Market)",
"15":"Option Execute",
"16":"Disposition (Non Open Market)",
"17":"Option Execute",
"18":"Automatic Sell",
"19":"Automatic Sell",
"20":"Disposition (Non Open Market)",
"21":"Option Execute",
"22":"Automatic Sell",
"23":"Disposition (Non Open Market)",
"24":"Automatic Sell"
},
"sharesTraded":{
"0":"20,200",
"1":"176,299",
"2":"8,053",
"3":"6,399",
"4":"13,136",
"5":"8,559",
"6":"16,612",
"7":"167,889",
"8":"13,250",
"9":"176,299",
"10":"177,870",
"11":"365,600",
"12":"189,301",
"13":"365,600",
"14":"184,461",
"15":"365,600",
"16":"189,301",
"17":"365,600",
"18":"96,735",
"19":"15,366",
"20":"16,530",
"21":"31,896",
"22":"25,000",
"23":"1,276",
"24":"25,000"
}
}
我当前的代码是尝试删除/过滤掉值为“处置(非公开市场)”的条目:
import json
data = json.load(open("AAPL22_institutional_table_MRKTVAL.json"))
modified = lambda feature: 'Disposition (Non Open Market)' not in feature['transactionType']
data2 = filter(modified, data)
open("AAPL22_institutional_table_MRKTVAL.json", "w").write(
json.dumps(data2, indent=4))
首选 output JSON(显示在所有 3 列上删除的条目):
{
"lastDate":{
"0":"11\/22\/2022",
"1":"10\/28\/2022",
"2":"10\/17\/2022",
"4":"10\/15\/2022",
"6":"10\/15\/2022",
"7":"10\/03\/2022",
"8":"10\/03\/2022",
"9":"10\/03\/2022",
"11":"10\/01\/2022",
"13":"10\/01\/2022",
"15":"10\/01\/2022",
"17":"10\/01\/2022",
"18":"08\/17\/2022",
"19":"08\/08\/2022",
"21":"08\/05\/2022",
"22":"08\/03\/2022",
"24":"05\/04\/2022"
},
"transactionType":{
"0":"Sell",
"1":"Automatic Sell",
"2":"Automatic Sell",
"4":"Option Execute",
"6":"Option Execute",
"7":"Automatic Sell",
"8":"Sell",
"9":"Automatic Sell",
"11":"Option Execute",
"13":"Option Execute",
"15":"Option Execute",
"17":"Option Execute",
"18":"Automatic Sell",
"19":"Automatic Sell",
"21":"Option Execute",
"22":"Automatic Sell",
"24":"Automatic Sell"
},
"sharesTraded":{
"0":"20,200",
"1":"176,299",
"2":"8,053",
"4":"13,136",
"6":"16,612",
"7":"167,889",
"8":"13,250",
"9":"176,299",
"11":"365,600",
"13":"365,600",
"15":"365,600",
"17":"365,600",
"18":"96,735",
"19":"15,366",
"21":"31,896",
"22":"25,000",
"24":"25,000"
}
}
我能够通过将具有字符串值的键附加到列表然后简单地删除它来根据值删除
import json
data = json.load(open("AAPL22_institutional_table_MRKTVAL.json"))
delete_keys = []
for value in data['transactionType']:
if data['transactionType'][value] == 'Disposition (Non Open Market)':
delete_keys.append(value)
print(delete_keys)
for key in delete_keys:
del data['transactionType'][key]
del data['lastDate'][key]
del data['sharesTraded'][key]
print(data)
open("AAPL22_institutional_table_MRKTVAL.json", "w").write(
json.dumps(data, indent=4))
data = { "lastDate":{ "0":"11\/22\/2022", "1":"10\/28\/2022", "2":"10\/17\/2022", "3":"10\/15\/2022", "4":"10\/15\/2022", "5":"10\/15\/2022", "6":"10\/15\/2022", "7":"10\/03\/2022", "8":"10\/03\/2022", }, "transactionType":{ "0":"Sell", "1":"Automatic Sell", "2":"Automatic Sell", "3":"Disposition (Non Open Market)", "4":"Option Execute", "5":"Disposition (Non Open Market)", "6":"Option Execute", "7":"Automatic Sell", "8":"Sell", }, "sharesTraded":{ "0":"20,200", "1":"176,299", "2":"8,053", "3":"6,399", "4":"13,136", "5":"8,559", "6":"16,612", "7":"167,889", "8":"13,250", } } for k,v in data["transactionType"].copy().items(): if v == "Disposition (Non Open Market)": for key in data: # Remove the key from all other nested dictionaries del data[key][k] print(data)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.