Getting Variables from a file

David Goodger dgoodger at bigfoot.com
Sat May 27 23:17:13 EDT 2000


on 2000-05-27 19:34, Curtis Jensen (cjensen at bioeng.ucsd.edu) wrote:
> I have a text file of the form:
> 
> (var = integer)

I assume that the above line is part of the description, not the file
itself.

> dof = 8
> elements = 10
> mesh_dof = 300
> .
> .
> .
> .
> 
> I want to read the file from within Python and then create a variable in
> python of the same name as what is in the text file, and holding the
> assigned value.  Assuming I don't already know the names of the
> varialbes; how do I do this?  I can do the file I/O, but how do I change
> a character string into a variable name?  Thanks.

If your variables text file conforms to Python syntax, use
execfile("filename"). But beware! Huge potential for abuse here!

-- 
David Goodger    dgoodger at bigfoot.com    Open-source projects:
 - The Go Tools Project: http://gotools.sourceforge.net
 (more to come!)




More information about the Python-list mailing list