Store variable name in another variable

Diez B. Roggisch deets at nospam.web.de
Tue May 1 10:49:11 EDT 2007


loial schrieb:
> OK, I have it working with dictionaries.
> 
> However I have another scenerio :
> 
> I am reading a file containing records like the following :
> 
> <IMPOSITION_DOCUMENT>
> <WFBAN_PAGE>
> <WFBAN_START_DETAILS name=\""MYVARIABLE"\">
> ..
> ..
> 
> 
> I need to substitute MYVARIABLE with the current value of MYVARIABLE
> in my python script and write the file out again.
> 
> The file may contain many more lines and many substitution values on
> any line
> 
> Assuming that MYVARIABLE is currently set to JOHN then the output
> would be
> 
> <IMPOSITION_DOCUMENT>
> <WFBAN_PAGE>
> <WFBAN_START_DETAILS name="JOHN">
> 
> Can this be done in Python? Amending the way the variable names are
> distinguished in the incoming file is possible if that would help.

There are many ways to do so in python. You can use the built-in string 
interpolation:

"<tag value="%(name)s"/>" % dict(name=value)

Or you can use one of the many available templating engines, like KID, 
genshi, cheetah and so on.

Diez



More information about the Python-list mailing list