I have a shapefile based on 27 locations, with historical values at each location for each month of the year going back to 1974. The file has 11,000 values. I have been trying to work on a script to select the 27 locations for each month of each year to eventually create shapefile for each monthly set of the 27 locations.

import arcpy
arcpy.env.overwriteOutput = True 
arcpy.env.workspace = "C://project"
LocationFile = "C://project//27location.shp"

FieldList = arcpy.ListFields("27location.shp")

for field in FieldList:
    print field.name
    # check for fields

cursor = arcpy.da.SearchCursor(Locationfile, ["NAME", "VALUE", "MONTH", "YEAR"])
for row in cursor:
    # select based on monthly values at the 27 locations into a seperate shapefile. 

I hope this makes sense I have been struggling to interpret code.

  • You'll probably get a better answer if you include 1) a link to your shapefile (put it on Google Drive e.g.), and 2) the expected output of your code. What do you envision the result to look like? An image is helpful. – Jon Jun 6 at 22:34
  • Don't double up your slashes like that. You need to double up on back-slashes (\) but not normal slashes (/). You can use either normal (forward) slashes or back-slashes in Python, but if you use back-slashes, you have to either escape them, ie, double them up (because the back-slash is an escape character itself) or use the r"" raw string notation. But for forward slashes in Python, you don't need to worry about any of that. – Son of a Beach Jun 6 at 23:13

You don't need to use a cursor for this.

You can create a layer using a 'where' clause so that it only includes the features that match your criteria. Then copy that layer to your new shapefile.

If fact, you can even skip creating a layer and you can use FeatureClassToFeatureClass_conversion() to do it all in one step as that function includes an optional 'where' clause. See: http://desktop.arcgis.com/en/arcmap/10.3/tools/conversion-toolbox/feature-class-to-feature-class.htm


import arcpy, os
projectDirectory = "C:/project"
LocationFile = os.path.join(projectDirectory, "27location.shp")

for year in MyListOfYears:
    for month in MyListOfMonths:
        outName = "{}_{}.shp".format(year, month)
        where = "YEAR={} AND MONTH={}".format(year, month)
        arcpy.FeatureClassToFeatureClass_conversion(LocationFile, projectDirectory, outName, where)

NB: If your year and month fields are strings, not numbers, you would need to use quotes in the where clause: where = "YEAR='{}' AND MONTH='{}'".format(year, month)

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.