I am working on a project that requires variables to be saved in a text file and then turned back into variables. For example, a text file containing
var = "123abc"
would be imported, and the variable and it's contents imported into the code, allowing me to read what the variable var contained. This works great using this code:
def get_save(filename):
result = {}
with open(filename) as f:
for line in f:
try:
line = line.split(' = ',1)
result[line[0]] = ast.literal_eval(line[1].strip())
except:
pass
return result
globals().update(get_save('SAVEFILE.TXT'))
However, I want to do something a little more complex. I would like to be able to create objects within this text file. Here is an example class:
class object():
def __intit__(self, var, var2):
self.var = var
self.var2 = var2
I would then like to be able to do this within my text file:
ob = object("abc", 123)
or:
obs = [object("def", 456), object("ghi", 789)]
and have that imported into my code, ready to be played with. I can think of no way of doing this, any help would be much appreciated!
EDIT: I would like the text file to be easily editable and readable
Thanks