Using a modification of one of the above you would get something like the following:
def read_next_object(file):
obj = {}
for line in file:
if not line.strip(): continue
line = line.strip()
key, val = line.split(": ")
if key in obj and key == "Object":
yield obj
obj = {}
obj[key] = val
yield obj
planets = {}
with open( "test.txt", 'r') as f:
for obj in read_next_object(f):
planets[obj["Object"]] = obj
print planets
Fix the case for the RootObject
and I believe this is the final dictionary that you are looking for from the example data that you have posted. It is a dictionary of planets where each planet is a dictionary of it's information.
print planets["Sun"]["Radius"]
Should print the value 20890260
The output from the above looks like the following:
{ 'Earth': { 'Object': 'Earth',
'Orbital Radius': '77098290',
'Period': '365.256363004',
'Radius': '6371000.0',
'Satellites': 'Moon'},
'Moon': { 'Object': 'Moon',
'Orbital Radius': '18128500',
'Period': '27.321582',
'Radius': '1737000.10'},
'Sun': { 'Object': 'Sun',
'Orbital Radius': '0',
'Radius': '20890260',
'RootObject': 'Sun',
'Satellites': 'Mercury,Venus,Earth,Mars,Jupiter,Saturn,Uranus,Neptune,Ceres,Pluto,Haumea,Makemake,Eris'}}