5

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?

9

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 ')
4

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, privacy policy and cookie policy

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