我有一個非常復雜的 JSON 檔案,我鼓勵您在此處復制粘貼以獲得更好的可視化效果。
{
"data":[
{
"title":"Title1",
"paragraphs":[
{
"context":"Context1",
"qas":[
{
"answers":[
{
"answer_start":515,
"text":"String text"
}
],
"question": "Why something something?",
"id":"5733be284776f41900661182"
},
{
"answers":[
{
"answer_start":505,
"text":"String something text"
}
],
"question": "Why?",
"id":"5733be284776f4190066345"
}
]
},
{
"context":"Context2",
"qas":[
{
"answers":[
{
"answer_start":515,
"text":"String text"
}
],
"question": "Why something something?",
"id":"5733be284776f41900661182"
},
{
"answers":[
{
"answer_start":505,
"text":"String something text"
}
],
"question": "Why?",
"id":"5733be284776f4190066345"
}
]
}
]
},
{
"title":"Title2",
"paragraphs":[
{
"context":"Context10",
"qas":[
{
"answers":[
{
"answer_start":585,
"text":"String text"
}
],
"question": "Why something something?",
"id":"5733be284776f41900661682"
},
{
"answers":[
{
"answer_start":545,
"text":"String something text"
}
],
"question": "Why?",
"id":"5733be284776f41900663"
}
]
},
{
"context":"Context7",
"qas":[
{
"answers":[
{
"answer_start":525,
"text":"String text"
}
],
"question": "Why something something?",
"id":"5733be284776f41982"
},
{
"answers":[
{
"answer_start":595,
"text":"String something text"
}
],
"question": "Why?",
"id":"5733be284776f419005"
}
]
}
]
}
],
"version":"1.1"
}
我不需要所有這些資訊,我希望我的資料格式看起來更簡單,如下所示:
[
{
"id": "5733be284776f41900661182",
"context": "Context1",
"question": "Why something something?",
"answers": {"text": ["String text"]}
},
{
"id": "5733be284776f4190066345",
"context": "Context1",
"question": "Why?",
"answers": {"text": ["String something text"]}
},
{
"id": "5733be284776f41900661182",
"context": "Context2",
"question": "Why something something?",
"answers": {"text": ["String text"]}
},
{
"id": "5733be284776f4190066345",
"context": "Context2",
"question": "Why?",
"answers": {"text": ["String something text"]}
},
{
"id": "5733be284776f41900661682",
"context": "Context10",
"question": "Why something something?",
"answers": {"text": ["String text"]}
},
{
"id": "5733be284776f41900663",
"context": "Context10",
"question": "Why?",
"answers": {"text": ["String something text"]}
},
{
"id": "5733be284776f41982",
"context": "Context7",
"question": "Why something something?",
"answers": {"text": ["String text"]}
},
{
"id": "5733be284776f419005",
"context": "Context7",
"question": "Why?",
"answers": {"text": ["String something text"]}
}
]
我不關心段落、標題、答案開始等,如果背景關系分開,我更喜歡它。有什么辦法可以將這樣的 JSON 變成更簡單的東西?我嘗試的是洗掉 Python 中不必要的單詞(用 替換它們" "),但它把它弄得一團糟。我還嘗試將其作為資料框打開并洗掉列,但是我也想洗掉許多嵌套的內容。
uj5u.com熱心網友回復:
您可以撰寫一個簡單的 Python 腳本來翻譯這種格式:
import json
with open('file.json', 'r') as fh:
data = json.load(fh)
result = []
for article in data["data"]:
for paragraph in article["paragraphs"]:
for qa in paragraph["qas"]:
answers = {"text": [answer["text"] for answer in qa["answers"]]}
result.append({
"id": qa["id"],
"context": paragraph["context"],
"question": qa["question"],
"answers": answers
})
with open('output.json', 'w') as fh:
json.dump(result, fh)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/358185.html
上一篇:通過跳過第一個鍵值來迭代字典
