May-22-2019, 06:09 PM
I'm trying to iterate through all of the questions and answers. What am I doing incorrectly?
This works and prints out entire JSON from the top of the hierarchy
print(data['data']['title'])
TypeError: list indices must be integers or slices, not str
This works and prints out entire JSON from the top of the hierarchy
import json with open('D:/trivia_json/dev-v2.0.json', encoding='utf-8') as data_file: data = json.loads(data_file.read()) for i in range (0, len (data['data'])): print(data['data'])This fails and show the error.
print(data['data']['title'])
TypeError: list indices must be integers or slices, not str
import json with open('D:/trivia_json/dev-v2.0.json', encoding='utf-8') as data_file: data = json.loads(data_file.read()) for i in range (0, len (data['data'])): print(data['data']['title'])-------------BELOW IS FIRST PART OF JSON
{ "version": "v2.0", "data": [ { "title": "Normans", "paragraphs": [ { "qas": [ { "question": "In what country is Normandy located?", "id": "56ddde6b9a695914005b9628", "answers": [ { "text": "France", "answer_start": 159 }, { "text": "France", "answer_start": 159 }, { "text": "France", "answer_start": 159 }, { "text": "France", "answer_start": 159 } ], "is_impossible": false }, { "question": "When were the Normans in Normandy?", "id": "56ddde6b9a695914005b9629", "answers": [ { "text": "10th and 11th centuries", "answer_start": 94 }, { "text": "in the 10th and 11th centuries", "answer_start": 87 }, { "text": "10th and 11th centuries", "answer_start": 94 }, { "text": "10th and 11th centuries", "answer_start": 94 } ], "is_impossible": false },