6

I am trying to copy specific attribute table rows from one shapefile to a new shapefile that will contain only the specified rows in its attribute table, that is, if a field GDP_TOP10 = 1.

I am using this at the moment:

#Grab rows using Search Cursor with WHERE clause
rows = arcpy.SearchCursor("C:/CGDP.shp", "GDP_TOP10 = 1")
for row in rows:
    arcpy.CopyFeatures_management("CGDP.shp", "New.shp")
del row
del rows

However, this just copies the entire shapefile, not the rows I want to select with the cursor. If I execute a print statement under my for loop instead, it works and prints only the relevant fields, but the copy to new shapefile command does not seem to copy specifically.

Is there a different ArcPy command to only copy rows that match a query?

0

2 Answers 2

10

I recommend using the Select_analysis tool which will accept a where clause.

arcpy.Select_analysis("C:/CGDP.shp", "C:/New.shp", ' "GDP_TOP10" = 1 ')
6

Create a feature layer with a selection, then use CopyFeatures:

arcpy.management.MakeFeatureLayer("C:/CGDP.shp", "new_layer", ' "GDP_TOP10" = 1 ')
arcpy.management.CopyFeatures("new_layer", "c:/new.shp")
arcpy.management.Delete("new_layer")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.