Problem with variables assigned to variables???

G

grepla

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?
 
L

Lutz Horn

Hi,

2008/4/30 said:
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:
.... 'name2':"\"field_a\" LIKE '031'",
.... 'name3':"\"field_a\" LIKE '041'"}.... print key, value
....
name2 "field_a" LIKE '031'
name3 "field_a" LIKE '041'
name1 "field_a" LIKE '021'

Lutz
 
M

M.-A. Lemburg

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)________________________________________________________________________

:::: 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
 
B

Bruno Desthuilliers

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top