I'm using ArcMap 10.2 and want a python script to loop through a list to make new feature classes, using the list items as attribute values in a selection before copying the features to a new file.
The script makes the first feature class fine but then copies all of the features for the next two iterations of the loop.
import arcpy
arcpy.env.workspace = r"C:\RS_Data\SHP"
arcpy.env.overwriteOutput = True
amenities = ['school', 'hospital', 'place_of_worship']
points = r"C:\RS_Data\SHP\OSMpoints.shp"
arcpy.MakeFeatureLayer_management(points, "OSM_Lyr")
with arcpy.da.SearchCursor("OSM_Lyr","amenity") as cursor:
for amenity in amenities:
query3 = '"amenity" = ' + "'" + amenity + "'"
arcpy.SelectLayerByAttribute_management("OSM_Lyr", "CLEAR_SELECTION")
for row in cursor:
if row[0] == amenity:
arcpy.SelectLayerByAttribute_management("OSM_Lyr", "ADD_TO_SELECTION",query3)
arcpy.CopyFeatures_management("OSM_Lyr", amenity)
print query3
Here is the print statement I get for the query after each loop:
"amenity" = 'school'
"amenity" = 'hospital'
"amenity" = 'place_of_worship'
It seems the selection is working the first time but not for the next two iterations. If I take the CLEAR_SELECTION line out it makes three identical feature classes using the same attribute value. How can I get the loop to make a selection on the next value in the list and copy the features to a new file?