I'm trying to read in a text file formatted like the following:
student
first name: John
last name: Doe
grade: 9
gpa: 4.0
school
name: Richard High School
city: Kansas City
####
student
first name: Jane
last name: Doe
grade: 10
gpa: 3.0
school
name: Richard High School
city: Kansas City
into a Python dictionary. Trying to have the end result look like:
{0:{'student':{'first name': 'John',
'last name': 'Doe',
'grade': '9',
'gpa': '4.0'},
"school": {'name': 'Richard High School',
'city': 'Kansas City'},
1:{'student':{'first name': 'Jane',
'last name': 'Doe',
'grade': '10',
'gpa': '3.0'},
'school': {'name': 'Richard High School',
'city': 'Kansas City'}
}
So far, I know how to handle the inner keys with:
with open('<filename>') as f:
dict = {}
for line in f:
x, y = line.split(": ")
dict[x] = y
print(dict)
But beyond that I'm stuck.