Line 24 fixed, final code now updated. Line 22 since fixed , now I get the error Traceback (most recent call last): File "C:\Users\shehn\Documents\ArcGIS\PyScripts\lab6X.py", line 24, in arcpy.Select_analysis (inputFile, outSHP, "\"FID\" = '" + each_attribute + "'") TypeError: cannot concatenate 'str' and 'int' objects
Here is what I tried to do when I looked at the existing way , I got the error : Traceback (most recent call last): File "C:\Users\shehn\Documents\ArcGIS\PyScripts\lab6X.py", line 22, in outSHP = outDir + each_attribute + u".shp" TypeError: coercing to Unicode: need string or buffer, int found
import arcpy
#Set OverWrite if files already exist
arcpy.env.overwriteOutput = True
#Set Input Output variables
inputFile = u"C:\\Users\shehn\\Documents\\2018\\LD ARCH 221\\lab 5\\burn2.shp"
outDir = u"C:\\Users\\shehn\\Documents\\2018\\LD ARCH 221\\lab 5"
# Reads My_shapefile for different values in the attribute
rows = arcpy.SearchCursor(inputFile)
row = rows.next()
attribute_types = set([])
while row:
attribute_types.add(row.FID)
row = rows.next()
# Output a Shapefile for each different attribute
for each_attribute in attribute_types:
outSHP = outDir + each_attribute + u".shp"
print outSHP
arcpy.Select_analysis (inputFile, outSHP, "FID = {0}".format(each_attribute))
del rows, row, attribute_types
I have to do this in ArcPy, otherwise I would have made a model in Model Builder. I am very new to Python. I think most of my code works, it seems to have an error at the CopyFeatures section. I am trying to get all 20 record in the FID column to become separate shapefiles so I have multiple polygons instead of one. I have tried the solution here "Exporting feature class into multiple feature classes based on field values using ArcGIS Desktop?" but it keeps giving me the expected string but got int error, so I tried doing it this way.
Here is my code:
# import system modules
import arcpy
# set workspace environment
env = arcpy.env.workspace = u"C:\\Users\shehn\\Documents\\2018\\LD ARCH 221\\lab 5\\burn2.shp"
#Set OverWrite if files already exist
arcpy.env.overwriteOutput = True
# Make a layer from the feature class
FC = arcpy.MakeFeatureLayer_management("burn2.shp", "FC")
#Create a list of years to iterate through to enable selection of each year
FIDlist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
print FIDlist
#Initiate Search Cursor to use in loop of attribute values
rows = arcpy.SearchCursor(FC)
for row in rows:
fieldName = "FID"
value = row.getValue(fieldName)
for field in FIDlist:
Year_layer = str(field) + "_"
arcpy.MakeFeatureLayer_management(FC, Year_layer)
arcpy.SelectLayerByAttribute_management (Year_layer, "NEW_SELECTION", "\"FID\" = %s" % (field))
arcpy.CopyFeatures_management(Year_layer, "fc_" + str(field))
outSHP = outDir + str(each_attribute) + u".shp"– PolyGeo Mar 29 '18 at 05:00arcpy.Select_analysis (inputFile, outSHP, "FID = {0}".format(each_attribute))– PolyGeo Mar 29 '18 at 05:28