我正在加载具有以下结构的JSON文件:
{"questions":
[{"question_id": 0, "image_filename": "CLEVR_val_009641.png", "question": "Are there any tiny green things made of the same material as the small brown sphere?"},
{"question_id": 1, "image_filename": "CLEVR_val_010849.png", "question": "Are there any purple metal things of the same size as the green shiny object?"},
{"question_id": 2, "image_filename": "CLEVR_val_004213.png", "question": "Is there a big thing of the same color as the big metal ball?"},
{"question_id": 3, "image_filename": "CLEVR_val_012597.png", "question": "There is a big metallic thing to the right of the blue rubber object; are there any small cyan rubber blocks that are to the right of it?"},
{"question_id": 4, "image_filename": "CLEVR_val_006304.png", "question": "Is there any other thing that has the same material as the small brown object?"}
]
}
我正在使用json.loads()
加载文件。
到目前为止,使用:
train_data_jsonload = json.load(f)
train_data = train_data_jsonload.get("questions")
我已经能够加载整个“数据集”。
我只想在我的json文件中加载question
键(所以所有问题都没有question_id
和image_filename
)
我尝试使用train_data.get("question")
,但收到错误list object has no attribute 'get'
我还尝试使用此处建议的内容:Nested dictionary,但未成功
那么,回到我的问题,我如何仅选择question
字段?
答案 0 :(得分:1)
try_this:
train_data_jsonload = json.load(f)
for questions_info in train_data_jsonload["questions"]:
question = question_info["question"]
#Do required stuff here if needed.
答案 1 :(得分:0)
如果要将问题列表作为输出,只需使用列表理解:
text_questions = [item["question"] for item in train_data_jsonload.get("questions")]
答案 2 :(得分:0)
最后我找到了解决方法:
for element in train_data:
print(element.get("question"))
将打印每个问题。