Hello,
I want to convert the contents of a string into name of variable.
For example:
var1="toto"
....
toto=5
print toto 6 2018
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>
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>
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
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/> <
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)
##############################
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 thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: perplexed |
last post by:
How do you convert a user inputted date to a unix timestamp before
insterting it into your database? I have a form, with a textfield for
a date that the user inputs in the format mm-dd-yyyy and...
|
by: IamZadok |
last post by:
Hi
I was wondering if anyone knew how to convert a string or an integer
into a Static Char.
Thx
|
by: Carl Gilbert |
last post by:
Hi
I have some ASP code that I want to run from CD within a VB.NET windows
application with a web browser control.
However, to get the ASP pages to run without a server is proving quite...
|
by: dale zhang |
last post by:
Hi,
I am trying to save and read an image from MS Access DB based on the
following article:
http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp
Right now, I saved images without any...
|
by: comp.lang.tcl |
last post by:
My TCL proc, XML_GET_ALL_ELEMENT_ATTRS, is supposed to convert an XML
file into a TCL list as follows:
attr1 {val1} attr2 {val2} ... attrN {valN}
This is the TCL code that does this:
set...
|
by: Robert Dufour |
last post by:
I got a stream from a file, could be any document type like doc or txt.
I want to convert it to a string variable so that I can replace some custom
tag contents in it's text with text values...
|
by: jehugaleahsa |
last post by:
I have a rather complex need.
I have a class that parses web pages and extracts all relevant file
addresses. It allows me to download every pdf on a web page, for
instance. I would like to...
|
by: Miro |
last post by:
How can I convert this part of the line:
Me.dgvmyData.Columns.Item("txtCellName") ' "txtCellName"
which is within this line -
Me.dgvmyData.Sort(Me.dgvmyData.Columns.Item("txtCellName"),...
|
by: Debadatta Mishra |
last post by:
Introduction
In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |