I'm trying to use arcpy to recreate clicking on a row in an attribute table, and exporting that row to a new class. I'm iterating through a feature class with 12,000+ records and filtering out features that meet a certain criteria (room classification). Using Select Layer By Attribute and Copy Features I'm getting every single row copied over. I think my where clause might be the issue.
Here is my snippet:
for row in arcpy.da.SearchCursor(fc, '*' ,sql_clause=(None, "WHERE CurrentSpaceClass = 'CR_Reg'")):
OID_field = arcpy.Describe(fc).OIDFieldName
site = row[8]
room_number = row[9]
pa_ratio = row[18]
angle = row[16]
site_no_space = site.replace(' ','_')
room_feature = scratch_gdb + site_no_space.lower() + "_room_" + room_number
where = '{0} = {1}'.format(arcpy.AddFieldDelimiters(fc,OID_field),row[0])
arcpy.SelectLayerByAttribute_management(fc,"NEW_SELECTION",where)
arcpy.CopyFeatures_management(fc,room_feature)
Solved! Go to Solution.
Ah duh, I just realized I needed to assign arcpy.SelectLayerByAttribute to a variable "selection" and then copy that. That did the trick!
If you print where (or AddMessage), is it what you're expecting?
Best guess:
where = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(fc,OID_field),row[0])
You should consider adding a check to see if anything is selected, otherwise, yes, you'll return the entire original feature class.
Ah duh, I just realized I needed to assign arcpy.SelectLayerByAttribute to a variable "selection" and then copy that. That did the trick!
Hi I’m trying to do the same thing any chance I can see your entire python script. I want to select layer by attribute with two conditions example like this x = 1 AND b = 2 and export each row that meets this criteria to a new shapefile. I also want to add the feature classes attributes from the row at end of the output name.