[GIS] Creating new shapefile based on its attributes with python

arcpyattribute-tableshapefile

I have a shapefile which has an attribute year. I used to generally copy and paste these features in the dataframe and then using the definition query just show polygons which were established before that year.

I want to know how can I do it using python. Like if I can give the input feature class, add all the years I want the different shapefiles for and the workspace where it can create shapefile for the mentioned years with the right naming convention.

Best Answer

I believe this was 'cross-posted' on another site... and the problem was answered with code below. (guidance by Mathew Coyle and Dan Patterson)

import arcpy
# input fc
fc = "D:\\2010.shp"
# the yr 'breaks'...hard-coded as a Python list.
yrs = [1991,1992,1996,1998,1999,2000,2003,2004,2005,2006,2008,2010]
# initialize variable (part of SQL qry) to remove previous selections
subtract = ''
for yr in yrs:
     where = '"Year" <= ' + str(yr) + subtract
     subtract = ' AND "Year" > ' + str(yr)
     filename = str(yr) + '.shp'
     arcpy.FeatureClassToFeatureClass_conversion(fc, "D:\\test output", filename, where)
Related Question