我有一個以下 Json 檔案格式,其中資料將進行動態更新。我必須在以下場景中使用 python 決議 Json 檔案
如果狀態: "PASS",則 value 的結果應該是maths,q1 ,q2
請在這個場景中使用 python
{
"quiz": {
"sport": {
"q1": {
"question": "Which one is correct team name in NBA?",
"options": [
"New York Bulls",
"Los Angeles Kings",
"Golden State Warriros",
"Huston Rocket"
],
"answer": "Huston Rocket"
},
"status": "BLOCK"
},
"maths": {
"q1": {
"question": "5 7 = ?",
"options": [
"10",
"11",
"12",
"13"
],
"answer": "12"
},
"q2": {
"question": "12 - 8 = ?",
"options": [
"1",
"2",
"3",
"4"
],
"answer": "4"
},
"status": "PASS"
}
}
}
uj5u.com熱心網友回復:
希望下面的代碼為您提供預期的輸出
import json
file = open('data.json')
data = json.load(file)
keys = []
for i in data['quiz']:
if data['quiz'][i]['status'] != 'PASS':
keys.append(i)
for key in keys:
del data['quiz'][key]
print(data)
for key in data['quiz'].keys():
print(key)
for key1 in data['quiz'][key].keys():
if key1 != 'status':
print(key1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/396793.html
上一篇:c語言中shell實作的問題
