My Objective: The purpose the program I am creating is to have the user enter the name of an element. Then python reads into an external file that finds the value that the requested element is assigned to and finally the value is printed out.
For example -
>>> helium
2
The problem is I don't know how to get python to interpretate the txt file that looks like this
hydrogen = 1
helium = 2
lithium = 3
as code. So when I enter print(lithium), I get an error.
My request: Could someone show me how I can get it to the point that I can read out the values and print them out. I don't need any help with the user input and all of that.
Thanks in advance.
UPDATE
I have used this code:
import json
file = open("noble_gases.json","r")
elements = json.loads(file.read())
noble_gases.json looks like this:
"helium" : 2,
"neon" : 10,
"argon" : 18,
"krypton" : 36,
"xenon" : 54,
"radon" : 86,
And I am now getting this error:
Traceback (most recent call last):
File "C:\Python34\Programs\Python Mini Project\finder.py", line 3, in <module>
elements = json.loads(file.read())
File "C:\Python34\lib\json\__init__.py", line 318, in loads
return _default_decoder.decode(s)
File "C:\Python34\lib\json\decoder.py", line 346, in decode
raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 1 column 10 - line 7 column 1 (char 9 - 85)
Thanks to everyone who has contrabuted. I am amazed by the speed of the responses.
UPDATE:
Removing the final comma in the json file did the trick. Thanks to everyone who helped. I can't give up ratings yet since i'm not level 15. So I gave you a thank you message instead.