Define a dictionary externally for a python program -
this question has answer here:
my python program reads pre-defined dictionary values processing. need able change values of dictionary. but, program converted executable file, should not defined inside program.
i thinking defining in text file. don't want write code parse , populate dictionary. need know if there way define dict in text file , assigning content directly python dict.
like this:
in text file:
{'a':1,'b':2,'c':3}
nb: actual 1 bit more enrich values. example.
in program: dict_variable = f.read()
so if print variable dictionary object:
{'a':1,'b':2,'c':3}
and can accessed dict_variable['a']
.
is there way this?
you can use eval() :
s = f.read()
d = eval(s)
from convert string representation of dictionary dictionary?
Comments
Post a Comment