select (and export) each record in an attribute table within a For: loop

1900
4
Jump to solution
12-02-2021 05:34 PM
PhilCrossley1
New Contributor III

Thanks for previous help!

I'm trying to write an ArcPy script that will:

iterate through a feature class consisting of all the counties in a particular state

select each county and create a new feature class consisting of just that boundary

so that I can then use each county's boundary to do a sequence of geoprocessing steps.

I think I need to begin with a SearchCursor, and then iterate through the list of values (County names) it returns, and I can do that (and print out all the county names as a test), but I can't figure out how to insert each countyName into a SelectBy Attribute query, within the for: loop, by referring to the cursor. 

So far, I have this snipped (preceded by the requisite set up steps, querying for IN Counties etc:

fc = r"E:\advAppScripting\testing\tornhazard\Indiana\Indiana.gdb\IN_counties"
cursor = arcpy.da.SearchCursor(fc, ["NAME"])
for row in cursor:
arcpy.MakeFeatureLayer_management(r"E:\advAppScripting\testing\tornhazard\Indiana\Indiana.gdb\IN_counties", "counties_lyr")
arcpy.SelectLayerByAttribute_management("counties_lyr", "NEW_SELECTION", """"NAME" = row[0]""")

 I know this WHERE clause is wrong, I just can't figure out what the value needs to be in order to select the county whose name is the first one in the cursor (and then the second, the third, etc) or if I should be going about this some other way....(Note: if I follow the "for row in cursor: with 

print("{0}".format(row[0]

, I DO get a list of all the IN_county names, so I think my SearchCursor is functioning correctly.

THanks, Phil

0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

you could shorten your script if you are splitting based on an attribute (counties)

Split By Attributes (Analysis)—ArcGIS Pro | Documentation


... sort of retired...

View solution in original post

4 Replies
RPGIS
by
Occasional Contributor III

One thing that stands out is you are trying to make a feature layer from a row. This is something you cannot do. Instead, there are a couple of ways you could do this (either by modelbuilder or python).

If you want to do this using python, use this instead:

for row in cursor:

    output_location = (folder path or file gdb location)

    country_name = row[0]

    arcpy.CreateFeatureclass_management(output_location, country_name, "POLYGON")

Do this for every row and it should provide you with what you are trying to get at. 

0 Kudos
PhilCrossley1
New Contributor III

that is what I was trying to do..(turn a row into a fc)..

But, a question: I thought that the CreateFeatureclass_management was only for creating empty feature classes; whereas I want to use the existing geometry of the county polygons (each row in the input fc) to create a new fc that consists of a single polygon).   Will the snippet you suggested do that?

0 Kudos
DanPatterson
MVP Esteemed Contributor

you could shorten your script if you are splitting based on an attribute (counties)

Split By Attributes (Analysis)—ArcGIS Pro | Documentation


... sort of retired...
PhilCrossley1
New Contributor III

once again, THANKS!  SplitByAttribute does exactly what I was hoping to accomplish--in a single line; without needing to employ a SearchCursor.

More than 1 thumb up emoji!

Thanks, Dan!

0 Kudos