I am trying to read from a file that contains elements that I am going to edit and store in a dictionary. However I am facing this error when running the code
def read_file(filename):
infile = open(filename, 'r')
elements = {}
for line in infile:
infile.readline()
words = line.split(';')
for i in range(len(words)):
element = words[i].split('-')[0].upper().strip()
density = words[i].split('-')[1].strip().replace(',', '')
elements[element] = float(density)
return elements
read_file('atm_moon.txt')
Error:
Traceback (most recent call last):
File "atm_moon.py", line 14, in <module>
read_file('atm_moon.txt')
File "atm_moon.py", line 9, in read_file
density = words[i].split('-')[1].strip().replace(',', '')
IndexError: list index out of range
the file looks like this: https://www.uio.no/studier/emner/matnat/ifi/IN1900/h20/ressurser/live_programmering/atm_moon.txt