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_
What you want appear to want is the following, but this is NOT RECOMMENDED:
>>> for line in open('dangerous.txt'):
... exec('%s = %s' % tuple(line.split(':', 1)))
...
>>> var_a
'home'
This creates somewhat similar behavior to PHP's register_globals and hence has the same security issues. Additionally, the use of exec
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.)
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. using globals().update(the_dict)
.
If you want it in a local dictionary instead (e.g. inside a function), you can do it like this:
for (n, v) in the_dict.items():
exec('%s=%s' % (n, repr(v)))
as long as it is safe to use exec
. If not, you can use the dictionary directly.
The other solutions posted here didn't work for me, because:
import *
didn't work for me, as i need a way to override them by choosing another fileSo I ended up using Configparser
and globals().update()
Test file:
#File parametertest.cfg:
[Settings]
#Comments are no Problem
test= True
bla= False #Here neither
#that neither
And that's my demo script:
import ConfigParser
cfg = ConfigParser.RawConfigParser()
cfg.read('parametertest.cfg') # Read file
#print cfg.getboolean('Settings','bla') # Manual Way to acess them
par=dict(cfg.items("Settings"))
for p in par:
par[p]=par[p].split("#",1)[0].strip() # To get rid of inline comments
globals().update(par) #Make them availible globally
print bla
It's just for a file with one section now, but that will be easy to adopt.
Hope it will be helpful for someone :)
If your data has a regular structure you can read a file in line by line and populate your favorite container. For example:
Let's say your data has 3 variables: x, y, i.
A file contains n of these data, each variable on its own line (3 lines per record). Here are two records:
384
198
0
255
444
2
Here's how you read your file data into a list. (Reading from text, so cast accordingly.)
data = []
try:
with open(dataFilename, "r") as file:
# read data until end of file
x = file.readline()
while x != "":
x = int(x.strip()) # remove \n, cast as int
y = file.readline()
y = int(y.strip())
i = file.readline()
i = int(i.strip())
data.append([x,y,i])
x = file.readline()
except FileNotFoundError as e:
print("File not found:", e)
return(data)