0

How to simplify this code and do some improvement to get each feature class record from the shapefile?

import arcpy

featureClass = r"C:\Users\TL\Desktop\812test\SplitAllRoutes_FacID\FacID1655.shp"

count = int(arcpy.GetCount_management(featureClass).getOutput(0)) 
# In this case, count = 6

input = featureClass
output0 = r"C:\Users\TL\Desktop\812test\test\Route0"
arcpy.Select_analysis(input, output0, "{0} = 0".format(arcpy.AddFieldDelimiters(input,"FID")))

input = featureClass
output1 = r"C:\Users\TL\Desktop\812test\test\Route1"
arcpy.Select_analysis(input, output1, "{0} = 1".format(arcpy.AddFieldDelimiters(input,"FID")))

input = featureClass
output2 = r"C:\Users\TL\Desktop\812test\test\Route2"
arcpy.Select_analysis(input, output2, "{0} = 2".format(arcpy.AddFieldDelimiters(input,"FID")))

input = featureClass
output3 = r"C:\Users\TL\Desktop\812test\test\Route3"
arcpy.Select_analysis(input, output3, "{0} = 3".format(arcpy.AddFieldDelimiters(input,"FID")))

input = featureClass
output4 = r"C:\Users\TL\Desktop\812test\test\Route4"
arcpy.Select_analysis(input, output4, "{0} = 4".format(arcpy.AddFieldDelimiters(input,"FID")))

input = featureClass
output5 = r"C:\Users\TL\Desktop\812test\test\Route5"
arcpy.Select_analysis(input, output5, "{0} = 5".format(arcpy.AddFieldDelimiters(input,"FID")))

My original plan is to change the code like this, but it doesn't work. Any suggestions?

import arcpy

featureClass = r"C:\Users\TL\Desktop\812test\SplitAllRoutes_FacID\FacID1655.shp"

count = int(arcpy.GetCount_management(featureClass).getOutput(0)) # In this case, count = 6

for i in range(count):
    input = featureClass
    output%s = r"C:\Users\TL\Desktop\812test\test\Route%s" % (i, i)
    arcpy.Select_analysis(input, output%s % i, "{0} = %s".format(arcpy.AddFieldDelimiters(input,"FID") % i))
1
  • i is not a string so formatting it as %s isn't going to be good, try %d instead or use "C:\Users\TL\Desktop\812test\test\Route{}".format(i) to format it. With Select_analysis I wouldn't AddFieldDelimiters, it doesn't need it, just 'FID = {}'.format(i) for the query. What error message did you get when you tried to run it? Aug 15, 2016 at 5:16

2 Answers 2

3

You're so close to the answer by yourself...

import arcpy

featureClass = r"C:\Users\TL\Desktop\812test\SplitAllRoutes_FacID\FacID1655.shp"
count = int(arcpy.GetCount_management(featureClass).getOutput(0))

for Idx in range(count):
    OutputFC = r"C:\Users\TL\Desktop\812test\test\Route{}".format(Idx)
    arcpy.Select_analysis(featureClass,OutputFC,'FID = {}'.format(Idx))

I suspect, expanding on my comment, your biggest problem is the string formatting; as i is a number you can't use %s (string) format token to insert it into a string. I would encourage you to use string.format method as it's not so fussy about types (like the rest of python) until you need to do fixed decimals or leading 0's.

Also, when it comes to selecting the various field delimiters aren't needed FIELD = 0 works just as well as "FIELD" = 0 and [FIELD] = 0 and wont cause problems by accidentally using the wrong one. You only need the field delimiters in cases like CalculateField (field to be calculated) and a few rare other cases; though you could argue it's good to get into the practice of using them.

1
  • Thanks. Your code works for me. Also, thanks for your comment, which teaches me a lot.
    – Tianxin Li
    Aug 15, 2016 at 13:47
1

The following code is improved to extract each feature class from multiple shapefiles, which I used to do the same thing as the combination of iteration feature class and copy features in the Model Builder.

import arcpy

#resetting the workspace
def find_all_fcs(workspace):
    prev_workspace = arcpy.env.workspace

    arcpy.env.workspace = workspace
    fcs = arcpy.ListFeatureClasses()

    arcpy.env.workspace = prev_workspace
    return fcs

filein=r"C:\Users\TL\Desktop\812test\SplitAllRoutes"
fcs = find_all_fcs(filein)

for fc in fcs:
    temp_name=filein+'\\'+fc
    count = int(arcpy.GetCount_management(temp_name).getOutput(0))
    arcpy.CreateFolder_management(r"C:\Users\TL\Desktop\812test\SeperateRoutes", "%s" % fc[:-4])
    output = r"C:\Users\TL\Desktop\812test\SeperateRoutes\%s" % fc[:-4]
    for i in range(count):

        input = temp_name
        outname=output+"\\"+fc[:-4]+'_'+str(i)+'.shp'
        where_clause = "FID="+str(i)
        arcpy.Select_analysis(input, outname,where_clause)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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