I have a file containing lines in this type of format.
Example 1: nextline = "DD:MM:YYYY INFO - 'WeeklyMedal: Hole = 1; Par = 4; Index = 2; Distance = 459; Score = { Player1 = 4 };" Example 2: nextline = "DD:MM:YYYY INFO - 'WeeklyMedal: Hole = 1; Par = 4; Index = 2; Distance = 459; Score = { Player1 = 4; Player2 = 6; Player3 = 4 };"
First, I split the line into ':', which gives me a list of 2 entries. I would like to split this line into a dictionary with a key and a value, but where the evaluation key has several subframes with a value.
Hole 1 Par 4 Index 2 Distance 459 Score Player1 4 Player2 6 Player3 4
So, I'm using something like this ...
split_line_by_semicolon = nextline.split(":") dictionary_of_line = dict((k.strip(), v.strip()) for k,v in (item.split('=') for item in split_line_by_semicolon.split(';'))) for keys,values in dictionary_of_line.items(): print("{0} {1}".format(keys,values))
However, I get an error in the score element of the line:
ValueError: too many values to unpack (expected 2)
I can set the split to '=' for this, so it stops after the first '='
dictionary_of_line = dict((k.strip(), v.strip()) for k,v in (item.split('=',1) for item in split_line_by_semicolon.split(';'))) for keys,values in dictionary_of_line.items(): print("{0} {1}".format(keys,values))
However, I am losing sub-values ββin braces. Does anyone know how I can achieve this multi-layered dictionary?