Getting Variables from a file

Jeff jam at quark.emich.edu
Sun May 28 01:49:05 CEST 2000


On Sat, May 27, 2000 at 04:34:28PM -0700, Curtis Jensen wrote:
> I have a text file of the form:
> 
> (var = integer)
> 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.
> 

use 'exec':

[~] [7:47pm] [jam at quark-pts/2] % python
Python 1.5.2 (#1, Sep 17 1999, 20:15:36)  [GCC egcs-2.91.66 19990314/Linux
(egcs- on linux-i386
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> exec("dof=8")
>>> dof
8

For more information, check here:
http://www.python.org/doc/current/ref/exec.html

Hope that helps.

Regards,
J
-- 
|| visit gfd <http://quark.emich.edu/>
|| psa member -- <http://www.python.org/psa/> 




More information about the Python-list mailing list