你不能这么给数据啊,给个开头,结尾的括号,都是靠一个一个对出来的。。。。。。。。你可以省略掉中间内容,但是结尾要有啊
import json
# data里面"is_impossible": "false",貌似用逻辑值就报错了,应该都是字符或者数值
data = {"version": "v2.0",
"data": [{"title": "Beyonc\u00e9",
"paragraphs": [{"qas":
[
{"question": "When did Beyonce start becoming popular?",
"id": "56be85543aeaaa14008c9063",
"answers": [{"text": "in the late 1990s", "answer_start": 269}],
"is_impossible": "false"
},
{"question": "What areas did Beyonce compete in when she was growing up?",
"id": "56be85543aeaaa14008c9065",
"answers": [{"text": "singing and dancing", "answer_start": 207}],
}
]
}]
}]
}
data = str(data).replace('\'', '"') # 要求全部为双引号
data = json.loads(str(data)) # 抓换为json格式
# 保存json,查看结构
# with open('data1.json','w',encoding='utf-8') as f:
# f.write(json.dumps(data))
q_a_list = data['data'][0]['paragraphs'][0]['qas']
qlist = [i['question'] for i in q_a_list]
alist = [i['answers'][0] for i in q_a_list]
print(qlist)
print(alist)
['When did Beyonce start becoming popular?', 'What areas did Beyonce compete in when she was growing up?']
[{'text': 'in the late 1990s', 'answer_start': 269}, {'text': 'singing and dancing', 'answer_start': 207}] |