AnsweredAssumed Answered

Loop: Select by attribute and write to attribute table

Question asked by jmrowley19 on Feb 20, 2017
Latest reply on Feb 20, 2017 by jmrowley19

Is there a way to loop this function so that I'm not retyping it each time? I want to select and write to the attribute table for 3 bands, but am lost on how to loop this. See code below:

 

import arcpy

arcpy.SelectLayerByAttribute_management("Strain Regions","NEW_SELECTION",'''"band_number" = 'Band 1' ''')
arcpy.SelectLayerByLocation_management("Earthquakes","COMPLETELY_WITHIN","Strain Regions",'',"ADD_TO_SELECTION")
arcpy.AddField_management("Earthquakes",'region2', "TEXT")
arcpy.CalculateField_management("Earthquakes","region2",'"band 1"')
arcpy.SelectLayerByAttribute_management("Strain Regions","CLEAR_SELECTION")
arcpy.SelectLayerByAttribute_management("Earthquakes","CLEAR_SELECTION")

 

arcpy.SelectLayerByAttribute_management("Strain Regions","NEW_SELECTION",'''"band_number" = 'Band 2' ''')
arcpy.SelectLayerByLocation_management("Earthquakes","COMPLETELY_WITHIN","Strain Regions",'',"ADD_TO_SELECTION")
arcpy.AddField_management("Earthquakes",'region2', "TEXT")
arcpy.CalculateField_management("Earthquakes","region2",'"band 2"')
arcpy.SelectLayerByAttribute_management("Strain Regions","CLEAR_SELECTION")
arcpy.SelectLayerByAttribute_management("Earthquakes","CLEAR_SELECTION")

 

arcpy.SelectLayerByAttribute_management("Strain Regions","NEW_SELECTION",'''"band_number" = 'Band 3' ''')
arcpy.SelectLayerByLocation_management("Earthquakes","COMPLETELY_WITHIN","Strain Regions",'',"ADD_TO_SELECTION")
arcpy.AddField_management("Earthquakes",'region2', "TEXT")
arcpy.CalculateField_management("Earthquakes","region2",'"band 3"')
arcpy.SelectLayerByAttribute_management("Strain Regions","CLEAR_SELECTION")
arcpy.SelectLayerByAttribute_management("Earthquakes","CLEAR_SELECTION")

Outcomes