For the sake of practicing how to become more fluent using dictionaries, I trying to write a program that reads the chemical composition of the lunar atmosphere and assign the elements and their estimated composition as a key-value pair like this "NEON 20":40000
The data file looks like this
Estimated Composition (night, particles per cubic cm):
Helium 4 - 40,000 ; Neon 20 - 40,000 ; Hydrogen - 35,000
Argon 40 - 30,000 ; Neon 22 - 5,000 ; Argon 36 - 2,000
Methane - 1000 ; Ammonia - 1000 ; Carbon Dioxide - 1000
And my code so far looks like this:
def read_data(filename):
dicti = {}
with open(filename,"r") as infile:
infile.readline()
for line in infile:
words = line.split(";")
dicti[words[0]] = f"{words[1]}"
for key in dicti:
print(key, dicti[key])
read_data("atm_moon.txt")
My question is:
- How do I split on both
"-"and";"? - How do I assign the elements and their estimated atmospheric composition as a key-value pair in a simple and elegant way from this data file?
- How do I make the element names all upper case?
Is there anyone who is kind enough to help a rookie out? All help is welcomed.