Store variable name in another variable

loial admin at loial.co.uk
Tue May 1 08:43:37 EDT 2007


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.







On 26 Apr, 22:02, Laurent Pointal <laurent.poin... at wanadoo.fr> wrote:
> Cameron Laird wrote:
> > In article <f0q89v$ug... at news2.u-psud.fr>,
> > Laurent Pointal  <laurent.poin... at limsi.fr> wrote:
> >>loial a ?it :
> >>> I need to store a list ofvariablenames in a dictionary or list. I
> >>> then later need to retrieve the names of the variables and get the
> >>> values from the named variables. The named variables will already have
> >>> been created and given a value.
>
> >>"Named variables will already have been created"... in what namespace ?
>
> >>Store a list of names -> dict/list (see tutorial)
>
> >>Then,
> >>use namespace.name
> >>or  getattr(namespace,"name")
> >>or  locals()["name"]
> >>or  globals()["name"]
>
> > admin, I want to be sure you understand the advice you've been given.
> > Yes, it is possible to "double dereference" through named variables;
> > HOWEVER, it is essentially *never* advantageous to do so in application
> > programming with Python (nor is it in Perl and PHP, despite what many
> > senior people there teach).  Use a dictionary, perhaps one with
> > multi-dimensional keys.
>
> Yes, I understand. I just reply to OP question not searching the reason why
> he wants to manage *variables* this way (he talk about dict, so I hope he
> know that they can be used to map names to values).
> So, this is not an advice, just technical ways related to the question as it
> was written.
>
> And yes, personnally i use dictionnaries for such purpose.
>
> A+
>
> Laurent.- Hide quoted text -
>
> - Show quoted text -





More information about the Python-list mailing list