AnsweredAssumed Answered

Select Layer by Attribute using an expression failing

Question asked by donnariebe on Aug 15, 2017
Latest reply on Aug 16, 2017 by donnariebe

Greetings, I am writing some code that puts in an expression into a SelectLayerByAttribute statement and am running into a problem. I am generating a running sequence of numbers (closNum) that are made into text. (using an SDE geodatabase FYI)

closNum = "'" + '{:05d}'.format(i) + "'"

When I print that out it looks like closNum = '00000'

I generate an expression statement expression = '"' + ' LABEL = ' + "'" + closNum + "'"+ '"' which prints out as " LABEL = '00000'"

What I would like to do is put that into my layer selection query arcpy.SelectLayerByAttribute_management(closureLyr, "NEW_SELECTION", expression)

So when I run that, I get an invalid expression error, but if I run arcpy.SelectLayerByAttribute_management(closureLyr, "NEW_SELECTION", " LABEL = '00000'"), it works just fine.

Somehow I am missing something but I can't figure out what.

Any help would be awesome

 

Actual code below

import string, arcpy
from arcpy import env
arcpy.env.overwriteOutput = True

inFC = 'Database Connections/GISTLMD on DNRHLN6341 as TLMD_FMB_MGR - FMB_RoadInventory.sde/GISTLMD.TLMD_DO.FMB_RoadClosures'
closureLyr = 'closureLyr'
count = 1
arcpy.MakeFeatureLayer_management(inFC,closureLyr)


for i in range(0,10,1):
#  print '{:05d}'.format(i)
 closNum = "'" +  '{:05d}'.format(i) + "'"
 expression = '"LABEL = '  +  closNum +  '"'
 
 arcpy.AddMessage(expression)
 arcpy.SelectLayerByAttribute_management(closureLyr, "NEW_SELECTION", expression)
 # arcpy.SelectLayerByAttribute_management(closureLyr, "NEW_SELECTION", "LABEL = '00000'")
 result = arcpy.GetCount_management(closureLyr)
 cnt = int(result.getOutput(0))
 if cnt == 0:
  arcpy.AddMessage(expression + ' is missing')
  count = count + 1
 #count = count + 1
 if count == 5 :
  break

 

Donna

Outcomes