stu9403

Using a list in a for loop SQL statement

Discussion created by stu9403 on Apr 14, 2011
Latest reply on Apr 14, 2011 by seanstock
Hi all,

I'm a novice at programming/scripting and am having problems using a list to select and clip a line file.  My problem is trying to pass the name of the list item into the SelectByLayerAttribute SQL statement.  The main part of the code is below and I bolded the portion of the SQL statement that is giving me trouble.  I am trying to select each department in the list one at a time and then clip by that selection.  The code works if I hard code a department name in the SQL statement.  Any help will be appreciated.  Thanks.

Stu

    ###Create Search Cursor
    cur = gp.SearchCursor("DeptFL")
    row = cur.Next()
    
    ###Create an empty list
    uniqueList = []
    
    while row:
        uniqueList.append(row.GetValue(InField))
        row = cur.Next()
    print uniqueList

    for dpt in uniqueList:
        gp.SelectLayerByAttribute_Management("DeptFL", "NEW_SELECTION", "\"NAME\" = " + '\'dpt\'')
        print dpt + " selected"
        gp.Clip_Analysis("SprayFL", "DeptFL", "test_" + dpt + ".shp")

Outcomes