|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
正在做一个问答系统的实例,引入一个json文件,里面包括了问题和答案,已经改成了dict格式。然后我想将这个文件里的question和answer分别添加到两个新的列表,因为文件里的每个对应question和answer都在一个小字典里,文件不是纯小字典组成,开头有其他内容。然后想问一下大神们,我应该怎么操作呢?
qList = []
aList = []
d = t_json
for i in t_json:
qList.append(i['question'])
aList.append(i['answers'])
print(qList)
print(aList)
这是我的代码,但是报错string indices must be integers
你不能这么给数据啊,给个开头,结尾的括号,都是靠一个一个对出来的。。。。。。。。你可以省略掉中间内容,但是结尾要有啊
- 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}]
|
|