Converting the value from string to integer in a nested dictionary
Posted
by
tom smith
on Stack Overflow
See other posts from Stack Overflow
or by tom smith
Published on 2012-11-24T04:59:10Z
Indexed on
2012/11/24
5:03 UTC
Read the original article
Hit count: 278
I want to change the numbers in my dictionary to int values for use later in my program. So far I have
import time
import math
x = 400
y = 300
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( "smallsolar.txt", 'r') as f:
for obj in read_next_object(f):
planets[obj["Object"]] = obj
print(planets)
scale=250/int(max([planets[x]["Orbital Radius"] for x in planets if "Orbital Radius" in planets[x]]))
print(scale)
and the output is
{'Sun': {'Object': 'Sun', 'Satellites': 'Mercury,Venus,Earth,Mars,Jupiter,Saturn,Uranus,Neptune,Ceres,Pluto,Haumea,Makemake,Eris', 'Orbital Radius': '0', 'RootObject': 'Sun', 'Radius': '20890260'}, 'Moon': {'Object': 'Moon', 'Orbital Radius': '18128500', 'Period': '27.321582', 'Radius': '1737000.10'}, 'Earth': {'Object': 'Earth', 'Satellites': 'Moon', 'Orbital Radius': '77098290', 'Period': '365.256363004', 'Radius': '6371000.0'}}
3.2426140709476178e-06
I want to be able to convert the numbers in the dict to ints for further use. Any help in greatly appreciated.
© Stack Overflow or respective owner