The dictionary you propose is poorly formed, you cannot have values without code (key). I will assume that the values must be a list of sublists:Incorrect dictionary{'Estudiante 1: Manuel Santos' : [Lengua, Matematicas, Sociales, Recreo], [8, 10, 9], ...}
Suposition, with correct structure:{'Estudiante 1: Manuel Santos' : [['Lengua', 'Matematicas', 'Sociales', 'Recreo'], [8, 10, 9]], ...}
Next code searches for a key in the lines that begin with "Estudiante". If that line doesn't start with that word, then it'll be the matter. This line is made replace with regular expressions. Replacement with : is arbitrary, but convenient, since the first line has a pseudodivision with two points, then it would be easy to make a partition key:value for every complete record and assemble a dictionary as one would like, like the one I describe in the last part of this post.From a line with the data:Lengua->8->Matematicas->10->Sociales->9->Recreo
After applying it re.sub() It will remain:Lengua:8,Matematicas:10,Sociales:9,Recreo
Finally that line is sent to a function which will divide into subchains separated by commas and then that subchain by the :. The line notas.append(None) You can remove it if you don't need that value. I put it to match the amount of matter with notes.import re
def split_data(li):
materias = []
notas = []
for elem in li.split(','):
pos = elem.find(':')
if pos > 0:
materias.append(elem[:pos])
notas.append(int(elem[pos+1:]))
else:
materias.append(elem)
notas.append(None)
return [materias, notas]
lista_notas = {}
key = ''
with open('stack019.txt', 'r') as f:
for line in f:
linea = line.strip()
if linea.startswith('Estudiante'):
key = linea
elif key != '' and linea != '':
data = re.sub(r'->([0-9]+)->', r':\1,', linea)
value = split_data(data)
lista_notas[key] = value
key = ''
else:
pass
print(lista_notas)
It results{'Estudiante 1: Manuel Santos': [['Lengua', 'Matematicas', 'Sociales', 'Recreo'], [8, 10, 9, None]],
'Estudiante 2: Iris Garcia': [['Lengua', 'Matematicas', 'Sociales', 'Recreo'], [5, 9, 4, None]]}
However, I consider that the above is not a well-structured dictionary anyway, so it is not very useful.A more descriptive dictionary, could have the data this way:estudiantes = {
"id":"1",
"nombre":"Manuel Santos",
"materias": {
"Lengua":8,
"Matematicas":10,
"Sociales":9,
"Recreo":None
},
"id":"2",
"nombre":"Iris Garcia",
"materias": {
"Lengua":5,
"Matematicas":9,
"Sociales":4,
"Recreo":None
}
}
But that will already require another modification in the code.