473,396 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem with variables assigned to variables???

I have a simple line of code that requires the following inputs - an
input file, output file and a SQL expression. the code needs to be
run with several different SQL expressions to produce multiple output
files. To do this I first created a list of a portion of the output
filename:
mylist = ('name1', 'name2', 'name3')

I also assigned variables for each SQL expression:
name1 = "\"field_a\" LIKE '021'"
name2 = "\"field_a\" LIKE '031'"
name3 = "\"field_a\" LIKE '041'"

Notice the variable names are the same as the listmember strings, that
is intentional, but obviously doesn't work.

the loop:
for listmember in mylist:
print listmember + ".shp", listmember

my intended output is:
name1.shp "field_a LIKE '021'
name2.shp "field_a LIKE '031'
name3.shp "field_a LIKE '041'

but, of course, the variable listmember returns the name of the
listmember which makes perfect sense to me:
name1.shp name1

So how can I iterate not only the filenames but the SQL expressions as
well?
Jun 27 '08 #1
4 942
Hi,

2008/4/30 <gr****@gmail.com>:
mylist = ('name1', 'name2', 'name3')

I also assigned variables for each SQL expression:
name1 = "\"field_a\" LIKE '021'"
name2 = "\"field_a\" LIKE '031'"
name3 = "\"field_a\" LIKE '041'"
my intended output is:
name1.shp "field_a LIKE '021'
name2.shp "field_a LIKE '031'
name3.shp "field_a LIKE '041'
You should use a dictionary someway like this:
>>mydict = {'name1':"\"field_a\" LIKE '021'",
.... 'name2':"\"field_a\" LIKE '031'",
.... 'name3':"\"field_a\" LIKE '041'"}
>>for key, value in mydict.items():
.... print key, value
....
name2 "field_a" LIKE '031'
name3 "field_a" LIKE '041'
name1 "field_a" LIKE '021'

Lutz

--
Do you want a Google Mail invitation? Just write me an email!
Jun 27 '08 #2
for listmember in mylist:
print listmember + ".shp", eval(listmember)
Jun 27 '08 #3
On 2008-04-30 07:25, gr****@gmail.com wrote:
I have a simple line of code that requires the following inputs - an
input file, output file and a SQL expression. the code needs to be
run with several different SQL expressions to produce multiple output
files. To do this I first created a list of a portion of the output
filename:
mylist = ('name1', 'name2', 'name3')

I also assigned variables for each SQL expression:
name1 = "\"field_a\" LIKE '021'"
name2 = "\"field_a\" LIKE '031'"
name3 = "\"field_a\" LIKE '041'"

Notice the variable names are the same as the listmember strings, that
is intentional, but obviously doesn't work.

the loop:
for listmember in mylist:
print listmember + ".shp", listmember

my intended output is:
name1.shp "field_a LIKE '021'
name2.shp "field_a LIKE '031'
name3.shp "field_a LIKE '041'

but, of course, the variable listmember returns the name of the
listmember which makes perfect sense to me:
name1.shp name1

So how can I iterate not only the filenames but the SQL expressions as
well?
The Python way to do this would be to take two lists, one
with the filenames and one with the SQL, and then iterate
over them in parallel:

for filename, sql_snippet in zip(filenames, sql_snippets):
...

(there are also a couple of ways to use iterators to do the
same)

If you just want to get you code to work, use this:

for listmember in mylist:
print listmember + ".shp", locals()[listmember]
--
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source (#1, Apr 30 2008)
>>Python/Zope Consulting and Support ... http://www.egenix.com/
mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
__________________________________________________ ______________________

:::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! ::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
Jun 27 '08 #4
n00m a écrit :
for listmember in mylist:
print listmember + ".shp", eval(listmember)
eval and exec are almost always the wrong solution. The right solution
very often implies a dict or attribute lookup, either on custom dict or
on one of the available namespaces (globals(), locals(), or a module,
class or instance).

Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Adam Short | last post by:
I'm having all sorts of problems with Sessions, I've been using them for years with out a hitch, all of a sudden the last 6 - 12 months since getting our new Win2003 server it's all gone shakey!!!...
2
by: Konrad Tuszkowski | last post by:
hello i have a form with 5 fields. and my problem is that if user put 2 or more same variables my script finds in db only 1 and i need to search all variables in db even if they r same. <http>...
15
by: Alfonso Morra | last post by:
Hi, I have some code from an example, that I want to retrofit into my project. The code from the example has the following line: SharedAppenderPtr myAppender( new...
7
by: vladislav.moltchanov | last post by:
I countinue to discover problems for simple tasks. Now I would like to write code, Sub or Function, where some values (more than 1) are calculated and are to be assigned to the variables which...
17
by: Archer | last post by:
I defined my class as: class List { public List Next; public int Data; public List() { Data=0; } }
78
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only...
3
by: Steven Bethard | last post by:
I'm trying to solve a constraint-satisfaction problem, and I'm having some troubles framing my problem in such a way that it can be efficiently solved. Basically, I want to build groups of two...
1
by: phoenix917 | last post by:
I have been working on this code for 5 days... I have read multiple tutorials and looked at various code. This is my third forum try... :) My instructor refuses to help me. I think it's because...
10
by: Dan | last post by:
Hi, I create 5 dropdownlist in code-behind. I want to insert their selectedvalues in a table by making a string separated with ":" I can get their selecedvalues but i'm stuck when i want to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.