Re: reading raw variables from file



"Astan Chee" schrieb
I have a file that might contain literal python
variable statements at every line. For example
the file info.dat looks like this:
users = ["Bob", "Jane"]
status = {1:"ok",2:users[0]}
the problem is I want to read this file and load
whatever variables written in it as normal python
variable statements so that when i read the file,
my users var will be ["Bob","Jane"] and my status
var will be {1:"ok",2:users[0]} .
Is there an easy way of doing this instead of
parsing the files and checking said types?


You might want to look at the eval, exec and execfile;
but bear in in mind Paddy's warning about security.

Regards
Martin


.



Relevant Pages

  • Re: reading raw variables from file
    ... variable statements at every line. ... the problem is I want to read this file and load ... If I have understood python naming scoping correctly, ... the script sets a useful default, ...
    (comp.lang.python)
  • Re: reading raw variables from file
    ... Martin Blume a écrit: ... the problem is I want to read this file and load whatever variables written in it as normal python variable statements so that when i read the file, ... Is there an easy way of doing this instead of parsing the files and checking said types? ...
    (comp.lang.python)