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 | |
Share this Question
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> | |
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>
| |
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 | |
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/> < | |
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)
############################## | |
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. | | This discussion thread is closed Replies have been disabled for this discussion. | | Question stats - viewed: 1718
- replies: 6
- date asked: Jul 18 '05
| |