Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm 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'm 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. Am I using the wrong script here? Is there a different arcpy. command to only copy rows that match a query?

share|improve this question
1  
This is more easily done by using MakeFeatureLayer with your SQL expression and then using CopyFeatures. –  PolyGeo Jun 2 '13 at 5:03
1  
Also, your where clause has a problem because it has nothing to indicate that GDP_TOP10 is a field name. –  PolyGeo Jun 2 '13 at 5:15
    
it maybe useful: copy attributes to new shapefile –  Ibe Jun 2 '13 at 17:49

2 Answers 2

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 ')
share|improve this answer

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")
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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