By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,957 Members | 2,038 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,957 IT Pros & Developers. It's quick & easy.

Convert the contents of a string into name of variable

P: n/a
Hello,
I want to convert the contents of a string into name of variable.
For example:

var1="toto"
....
toto=5
print toto
Jul 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Erwan VITIERE wrote:
I want to convert the contents of a string into name of variable.
For example:

var1="toto"
...
toto=5
print toto


why?

Python works better if you use it to write Python code. the Python
solution is to use a dictionary:

key1 = "toto"

data = {}
data["toto"] = 5

print data[key1]

</F>

Jul 18 '05 #2

P: n/a
Because i don't want to use this syntax because it is too long, I want a
direct access :

Not :
DicoUser['TOTO'].DicoTable.['MY_TABLE'].DicoLabel.['MY_LABEL'] = "for
exemple"

Finally, i want to use :
TOTO.MY_TABLE.MY_LABEL = "for exemple"

"Fredrik Lundh" <fr*****@pythonware.com> a écrit dans le message de news:
ma*************************************@python.org...
Erwan VITIERE wrote:
I want to convert the contents of a string into name of variable.
For example:

var1="toto"
...
toto=5
print toto


why?

Python works better if you use it to write Python code. the Python
solution is to use a dictionary:

key1 = "toto"

data = {}
data["toto"] = 5

print data[key1]

</F>

Jul 18 '05 #3

P: n/a

Erwan VITIERE wrote:
Because i don't want to use this syntax because it is too long, I want a direct access :

Not :
DicoUser['TOTO'].DicoTable.['MY_TABLE'].DicoLabel.['MY_LABEL'] = "for exemple"

Finally, i want to use :
TOTO.MY_TABLE.MY_LABEL = "for exemple"


setattr might be what you are looking for

Jul 18 '05 #4

P: n/a
On Thu, 24 Mar 2005 17:43:21 +0100, "Erwan VITIERE" <e.*******@free.fr>
declaimed the following in comp.lang.python:
Because i don't want to use this syntax because it is too long, I want a
direct access :

Not :
DicoUser['TOTO'].DicoTable.['MY_TABLE'].DicoLabel.['MY_LABEL'] = "for
exemple"

Finally, i want to use :
TOTO.MY_TABLE.MY_LABEL = "for exemple"
What's with the dotted notation? Looks like a dialect of REXX's
stem variables. Though even those do not do variable translation for the
first item -- you couldn't do

aUser = "TOTO"
aUser.MY_TABLE.MY_LABEL = ...

Yes, I know Python has dotted notation, but for accessing
members of modules or class instances, and I'd be leery of any code that
was using string variables to access into those.

What sort of data structure are you trying to access?

-=-=-=-=-=-=-=-

# need to initialize some test data first
tLabel = {"MY_LABEL" : "Some junk",
"OTHER LABEL" : "Other junk"}

tTable = {"MY_TABLE" : tLabel,
"OTHER_TABLE" : {"THIRD LABEL" : "More junk"} }

Users = {"TOTO" : tTable}

print

# Okay, put the strings into variables
aUser = "TOTO"
aTable = "MY_TABLE"
aLabel = "MY_LABEL"

# access something in the data
print Users[aUser][aTable][aLabel]

# change something in the data
Users[aUser][aTable][aLabel] = "For Example"

# show the whole structure
print Users

#access the changed item, using literals and variables mixed
print Users["TOTO"][aTable]["MY_LABEL"]

-=-=-=-=-=-=-=-=-

Some junk
{'TOTO': {'OTHER_TABLE': {'THIRD LABEL': 'More junk'}, 'MY_TABLE':
{'MY_LABEL': 'For Example', 'OTHER LABEL': 'Other junk'}}}
For Example

The REXX equivalent would look something like (I've not done
REXX in a decade):

Users."TOTO"."MY_TABLE"."MY_LABEL" = "Some junk"
Users."TOTO"."MY_TABLE"."OTHER LABEL" = "Other junk"
Users."TOTO". "OTHER_TABLE"."THIRD LABEL" = "More junk"

aUser = "TOTO"
aTable = "MY_TABLE"
aLabel = "MY_LABEL"

say Users.aUser.aTable.aLabel

Users.aUser.aTable.aLabel = "For example"

/* no equivalent for printing the entire structure */

say Users."TOTO".aTable."MY_LABEL"

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #5

P: n/a
TRy db_row does exactly what you want to do. Slower, but more simple:
##############################
#Sequence2Struct.py

class Struct:
pass

def MakeStruct(seq, names):
obj = Struct()
if len(seq) != len(names):
raise IndexError("seq and names are not the same length")
for i in range(len(names)):
obj.__dict__[names[i]] = seq[i]
return obj

def ExtractNames(t):
return [item[0] for item in t]
if __name__ == "__main__":
t = (1, 2, 3)
n1 = (("A", 1), ("B", 1), ("C", 2))
n = ExtractNames(n1)
print n
s = MakeStruct(t, n)
print s, s.A, s.B, s.C
n = ("A", "B")
s1 = MakeStruct(t, n)
##############################

Jul 18 '05 #6

P: n/a
Erwan VITIERE a écrit :
Hello,
I want to convert the contents of a string into name of variable.
For example:

var1="toto"
...
toto=5
print toto


exec "toto = 5"
print toto

But I would use another solution if possible.
Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.