1

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

closed as off-topic by Vince, whyzar, nmtoken, Dan C, xunilk Mar 29 '18 at 14:43

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This problem cannot or can no longer be reproduced. Changes to the system or to the asker's circumstances may have rendered the question obsolete, or the question does not include a procedure to enable potential answerers to reproduce the same symptoms. Such questions are off-topic as they are unlikely to help future readers, but editing them to include more details can lead to re-opening." – Vince, whyzar, nmtoken, Dan C, xunilk
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    I hate seeing the MakeFeatureLayer/SelectByAttribute/CopyFeatures coding pattern when Select_analysis() is so much easier - see gis.stackexchange.com/q/68018/115 for an example. Also, I see no need to use a SearchCursor here, and if you do, then use arcpy.da.SearchCursor() rather than the old arcpy.SearchCursor(). – PolyGeo Mar 29 '18 at 4:39
  • From the duplicate this is the answer that seems closest to the way that you are trying to do it: gis.stackexchange.com/a/152165/115 – PolyGeo Mar 29 '18 at 4:42
  • If you find anything that "seems to have an error" it is always best to include the full error message including line number. – PolyGeo Mar 29 '18 at 4:47
  • @PolyGeo, apologies, I'll add that now. – Shehnaz Mannan Mar 29 '18 at 4:48
  • Try outSHP = outDir + str(each_attribute) + u".shp" – PolyGeo Mar 29 '18 at 5:00