2

I am trying to get arcpy to select features in a shapefile that appear in a list generated from a model output file in a variable called 'died'.

The list looks like this:

died
['acadian022', 'acadian051', 'acadian052', 'acadian121', 'acadian152', 
'acadian191', 'chick031', 'chick032', 'chick052', 'chick132', 'creeper032', 
'downy032', 'downy042', 'kentucky041', 'ovenbird012', 'ovenbird061', 
'pewee011', 'pewee021', 'pewee032', 'pewee051', 'pewee071', 'pewee082', 
'pewee101', 'thrush022', 'thrush042', 'thrush152']

I can get arcpy to select one of these points using this:

inFeatures = "nests"
outFeatures = "C:/test.gdb/new_nests"
tempLayer = "nestslyr"
expression = "ID = 'acadian022'"

arcpy.CopyFeatures_management(inFeatures, outFeatures)
arcpy.MakeFeatureLayer_management(outFeatures, tempLayer)  
arcpy.SelectLayerByAttribute_management(tempLayer,"ADD_TO_SELECTION",
expression)

But when I change this to a for loop, the loop runs fine and I do not get any errors, but the tool does not select any points.

for x in died:
    expression = "ID = 'x'"
    arcpy.SelectLayerByAttribute_management(tempLayer,"ADD_TO_SELECTION",
    expression)

Does anyone know why this might be the case?

Laura
  • 21
  • 2
  • 1
    It's not working because your expression is not using the variable x, it is using the string x. See this answer about building expressions using variables: https://gis.stackexchange.com/questions/27457/including-variable-in-where-clause-of-arcpy-select-analysis – Jacob F Oct 05 '17 at 14:57
  • that's what I needed. I was googling and searching around here for hours and never found this, so thank you! – Laura Oct 05 '17 at 15:00

0 Answers0