Referring on this question, I have a similar -but not the same- problem..
On my way, I'll have some text file, structured like:
var_a: 'home'
var_b: 'car'
var_c: 15.5
And I need that python read the file and then create a variable named var_a with value 'home', and so on.
Example:
#python stuff over here
getVarFromFile(filename) #this is the function that im looking for
print var_b
#output: car, as string
print var_c
#output 15.5, as number.
Is this possible, I mean, even keep the var type?
Notice that I have the full freedom to the text file structure, I can use the format I like if the one I proposed isn't the best.
EDIT: the ConfigParser can be a solution, but I don't like it so much, because in my script I'll have then to refer to the variables in the file with
config.get("set", "var_name")
But what I'll love is to refer to the variable directly, as I declared it in the python script...
There is a way to import the file as a python dictionary?
Oh, last thing, keep in mind that I don't know exactly how many variables would I have in the text file.
Edit 2: I'm very interested at stephan's JSON solution, because in that way the text file could be read simply with others languages (PHP, then via AJAX JavaScript, for example), but I fail in something while acting that solution:
#for the example, i dont load the file but create a var with the supposed file content
file_content = "'var_a': 4, 'var_b': 'a string'"
mydict = dict(file_content)
#Error: ValueError: dictionary update sequence element #0 has length 1; 2 is required
file_content_2 = "{'var_a': 4, 'var_b': 'a string'}"
mydict_2 = dict(json.dump(file_content_2, True))
#Error:
#Traceback (most recent call last):
#File "<pyshell#5>", line 1, in <module>
#mydict_2 = dict(json.dump(file_content_2, True))
#File "C:\Python26\lib\json\__init__.py", line 181, in dump
#fp.write(chunk)
#AttributeError: 'bool' object has no attribute 'write'
In what kind of issues can I fall with the JSON format? And, how can I read a JSON array in a text file, and transform it in a python dict?
P.S: I don't like the solution using .py files; I'll prefer .txt, .inc, .whatever is not restrictive to one language.
hbn's answer won't work out of the box if the file to load is in a subdirectory or is named with dashes.
In such a case you may consider this alternative :
Or the simpler but deprecated in python3 :
I guess Stephan202's warning applies to both options, though, and maybe the loop on lines is safer.
Load your file with JSON or PyYAML into a dictionary
the_dict
(see doc for JSON or PyYAML for this step, both can store data type) and add the dictionary to your globals dictionary, e.g. usingglobals().update(the_dict)
.If you want it in a local dictionary instead (e.g. inside a function), you can do it like this:
as long as it is safe to use
exec
. If not, you can use the dictionary directly.How reliable is your format? If the seperator is always exactly ': ', the following works. If not, a comparatively simple regex should do the job.
As long as you're working with fairly simple variable types, Python's eval function makes persisting variables to files surprisingly easy.
(The below gives you a dictionary, btw, which you mentioned was one of your prefered solutions).
What you want appear to want is the following, but this is NOT RECOMMENDED:
This creates somewhat similar behavior to PHP's
register_globals
and hence has the same security issues. Additionally, the use ofexec
that I showed allows arbitrary code execution. Only use this if you are absolutely sure that the contents of the text file can be trusted under all circumstances.You should really consider binding the variables not to the local scope, but to an object, and use a library that parses the file contents such that no code is executed. So: go with any of the other solutions provided here.
(Please note: I added this answer not as a solution, but as an explicit non-solution.)
Assuming you're happy to change your syntax slightly, just use python and import the "config" module.
Then do
And you can reference them by name in your current context.
Use ConfigParser.
Your config:
Your python code: