我是编写家庭项目的初学者。我已经使用json来存储一些信息,现在我已经从另一个python函数中打开了要使用的信息,这是返回的信息:
{'_id': {'$oid': '5eab443d1eae7f1c689ab614'}, 'a': 'James', 'answer1': 'c', 'answer2': 'a', 'answer3': 'b', 'answer4': 'c', 'answer5': 'b', 'b': 'John', 'c': 'Jake', 'd': 'a house', 'e': 'a caravan', 'f': 'a hotel', 'g': 'red', 'h': 'orange', 'i': 'yellow', 'j': 'hat', 'k': 'scarf', 'l': "gloves", 'm': 'happy', 'n': 'sad', 'o': 'upset', 'q1': '1', 'q2': '2', 'q3': '3', 'q4': '4', 'q5': '5', 'question1': 'The boy's name was:', 'question2': 'The boy lived in:', 'question3': 'The boy's jumper was:', 'question4': 'The boy lost his:', 'question5': 'The boy was:', 'quiz_id': '1a', 'quiz_title': 'The New Boy', 'url': '/images/comp_cards/card1a.png'}
现在,我正在尝试从此列表中提取信息,但是所有信息都存储在0索引处,所以当我这样做时:
with open('quiz.json', 'r') as read_file:
quiz_data = json.load(read_file)
print(quiz_data[0])
全部退回,如果我将索引值更改为0以上,则不返回任何内容。我需要做什么才能仅从所需列表中检索数据,例如说我需要字段名称“ answer1”,“ answer2”和“ answer3”?谢谢
答案 0 :(得分:0)
由于输出是字典,因此您可以访问键并获取值。例如,如果您需要答案1的值,则可以尝试
quiz_data[0].get("answer1")
或
quiz_data[0]["answer1"]