This is so simple and effective I can't believe I didn't think to use python to make the variable to just pass as the whole where_clause. I've been working forever on this and finally saw your post!
Here is the correct where clause:for dat in datelst: arcpy.SelectLayerByAttribute_management ("hotspots", "NEW_SELECTION", "\"date\" = " + "'" + dat + "'")
for dat in datelst: arcpy.SelectLayerByAttribute_management ("hotspots", "NEW_SELECTION", "\"date\" = " + "'" + dat + "'")
whereExpr = "\"date\" = \'%s\'" % dat # I use outside double-quotes always for stylistic reasons whereExpr = '"date" = \'%s\'' % dat # but this is equivalent, using ' to preserve " inside the string arcpy.SelectLayerByAttribute_management ("hotspots", "NEW_SELECTION",whereExpr)
whereExpr = "\"date\" = \'{0}\'".format(dat)
In arcpy (and also when using gp = arcgisscripting.create(9.3)), you shouldn't need to use the "\" things in SQL expresions anymore
whereExpr = "{0} = \'{1}\'".format(arcpy.AddFieldDelimiters(datefield,wks),dat)
sList = ['BG','PP','IDFxh1','IDFxh1a','IDFxh2','IDFxh2a'] mLIST = ['IDFdk1','IDFdk1a','IDFdk2','IDFdk3','IDFunk','MS'] dList = ['ESSF','ICH','CWH'] mergeList = sList+mLIST+dList # This is using python list comprehension to add the '%' wildcard to each item in my list # because the attributes BG, PP, MS, ESSF, ICH, CWH contain up to 4 more characters mergeList = [x[:1]=='%' and x or x+'%' for x in mergeList] print "The merged lists look like this:\n " + str(mergeList) arcpy.MakeTableView_management("becFreqTbl","tempBecTbl") for list in mergeList: print 'iterating through list item:', list where_clause = "MAP_LABEL LIKE '"+list+"'" arcpy.SelectLayerByAttribute_management("tempBecTbl","ADD_TO_SELECTION",where_clause) newSel = int(arcpy.GetCount_management("tempBecTbl").getOutput(0)) print "The new selection yieled "+str(newSel)+" fields selected" arcpy.SelectLayerByAttribute_management("tempBecTbl","SWITCH_SELECTION") switchSel = int(arcpy.GetCount_management("tempBecTbl").getOutput(0)) print "The switch selection yielded "+str(switchSel)+" fields selected" arcpy.DeleteRows_management("tempBecTbl") print "Deleted "+str(switchSel)+" rows from table that did not match the list criteria"
The merged lists look like this: ['BG%', 'PP%', 'IDFxh1%', 'IDFxh1a%', 'IDFxh2%', 'IDFxh2a%', 'IDFdk1%', 'IDFdk1a%', 'IDFdk2%', 'IDFdk3%', 'IDFunk%', 'MS%', 'ESSF%', 'ICH%', 'CWH%']iterating through list item: BG%iterating through list item: PP%iterating through list item: IDFxh1%iterating through list item: IDFxh1a%iterating through list item: IDFxh2%iterating through list item: IDFxh2a%iterating through list item: IDFdk1%iterating through list item: IDFdk1a%iterating through list item: IDFdk2%iterating through list item: IDFdk3%iterating through list item: IDFunk%iterating through list item: MS%iterating through list item: ESSF%iterating through list item: ICH%iterating through list item: CWH%The new selection yieled 37 fields selectedThe switch selection yielded 11 fields selectedDeleted 11 rows from table that did not match the list criteria
for dat in datelst: arcpy.SelectLayerByAttribute_management ("hotspots", "NEW_SELECTION", "date = '" + dat + "'")
for dat in datelist: whereclause = "'date' = " + dat arcpy.SelectLayerByAttribute_management ("hotspots", "NEW_SELECTION", whereclause)
for dat in datelist: arcpy.SelectLayerByAttribute_management ("hotspots", "NEW_SELECTION", "'date' = '%s'"%dat)
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.