Problem with variables assigned to variables???
M.-A. Lemburg
mal at egenix.com
Wed Apr 30 06:18:36 EDT 2008
On 2008-04-30 07:25, grepla at 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
More information about the Python-list
mailing list