I am having issues reading a text file into Python, i am using a loop and it keeps stopping before the end of the text file as well as skipping the first line of the file, i’m sorting it into a dictionary as i read each line.My code is:
for x in file.readline():
s=file.readline().rstrip()
s=s.split(': ')
if s[0]=='RootObject':
Root=s[1]
if s[0]=='Object':
data[s[1]]={}
Ob=s[1]
elif s[0]=='Satellites':
data[Ob][s[0]]=s[1]
elif s[0]=='Orbital Radius':
data[Ob][s[0]]=float(s[1])
elif s[0]=='Period':
data[Ob][s[0]]=float(s[1])
elif s[0]=='Radius':
data[Ob][s[0]]=float(s[1])
It doesn’t make sense to iterate over
file.readline()like that. You should iterate over the lines instead like this: