Problem with variables assigned to variables???

Discussion in 'Python' started by grepla@gmail.com, Apr 30, 2008.

  1. Guest

    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?
     
    , Apr 30, 2008
    #1
    1. Advertising

  2. Lutz Horn Guest

    Hi,

    2008/4/30 <>:
    > 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!
     
    Lutz Horn, Apr 30, 2008
    #2
    1. Advertising

  3. n00m Guest

    for listmember in mylist:
    print listmember + ".shp", eval(listmember)
     
    n00m, Apr 30, 2008
    #3
  4. On 2008-04-30 07:25, 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
     
    M.-A. Lemburg, Apr 30, 2008
    #4
  5. 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).
     
    Bruno Desthuilliers, Apr 30, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. AndyAtHome
    Replies:
    1
    Views:
    595
    AndyAtHome
    May 25, 2004
  2. =?iso-8859-15?Q?Fabr=EDcio_de_Novaes_Kucinskis?=

    Session Variables assigned to the wrong session?

    =?iso-8859-15?Q?Fabr=EDcio_de_Novaes_Kucinskis?=, Jan 20, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    714
    Alvin Bruney [MVP]
    Jan 20, 2005
  3. Robert Faulkner
    Replies:
    0
    Views:
    882
    Robert Faulkner
    Jan 28, 2005
  4. jamaj

    Pointer assigned by a function problem...

    jamaj, Sep 3, 2008, in forum: C Programming
    Replies:
    13
    Views:
    459
  5. Stephan Wehner
    Replies:
    2
    Views:
    122
    Trans
    Aug 28, 2006
Loading...

Share This Page