Export features to new shapefile

747
5
Jump to solution
01-23-2013 04:58 AM
ChrisBrannin
Occasional Contributor
I am trying to write a small script to export each feature in a featureclass to a new shapefile. When I run it, all I see is red (errors). Any help would be greatly appreciated.

import arcpy from arcpy import analysis, env   fc = arcpy.GetParameterAsText(0) field = arcpy.GetParameterAsText(1) env.workspace = arcpy.GetParameterAsText(2)   with arcpy.da.SearchCursor(fc, (field,)) as cursor:     for row in cursor:         arcpy.MakeFeatureLayer_management(field)
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable
Sorry, my test was on strings.  I did not include functionality for other types.  Try this:

import arcpy from os import path as p arcpy.overwriteOutput = True   fc = r'F:\shape_clean_python\rioDej_fav.shp' field = 'FID' outws = r'F:\shape_clean_python\exports'   valList = [] with arcpy.da.SearchCursor(fc,[field]) as rows:     for row in rows:         value = row[0]         if value not in valList:             valList.append(value)    for val in valList:         for fld in arcpy.ListFields(fc):             if fld.name == field:                 if fld.type == 'String':                     query = '"{0}" = \'{1}\''.format(field,val)                 else:                     query = '"{0}" = {1}'.format(field,val)              output = p.join(outws,str(val).replace(' ','_'))         desc = arcpy.Describe(outws)         if desc.workspaceType == 'FileSystem':             output += '.shp'         if arcpy.Exists(output):             arcpy.Delete_management(output)         arcpy.Select_analysis(fc, output, query)         print 'Created "%s"' %p.basename(output)

View solution in original post

0 Kudos
5 Replies
by Anonymous User
Not applicable
Something like this should work.  I changed the env workspace to just a user specified output workspace.  I also chose to name the ouput feature class to the name of the value from the field.
import arcpy
from os import path as p
arcpy.overwriteOutput = True

fc = arcpy.GetParameterAsText(0)
field = arcpy.GetParameterAsText(1)
outws = arcpy.GetParameterAsText(2)

valList = []
with arcpy.da.SearchCursor(fc,[field]) as rows:
    for row in rows:
        value = row[0]
        if value not in valList:
            valList.append(value)

for val in valList:
    query = '"{0}" = \'{1}\''.format(field,val)
    output = p.join(outws,val.replace(' ','_'))
    desc = arcpy.Describe(outws)
    if desc.workspaceType == 'FileSystem':
        output += '.shp'
    if arcpy.Exists(output):
        arcpy.Delete_management(output)
    arcpy.Select_analysis(fc, output, query)
    print 'Created "%s"' %p.basename(output)


EDIT: I changed the code to match 10.1 with da cursors.  It also does a check on the workspace type to add the '.shp' if necessary.
0 Kudos
ChrisBrannin
Occasional Contributor
Sorry for the bad question here but. I am seeing this error:

   output = p.join(outws,val.replace(' ','_'))
AttributeError: 'int' object has no attribute 'replace'


I am not familiar with val.replace(). Thanks again.
0 Kudos
by Anonymous User
Not applicable
Sorry, my test was on strings.  I did not include functionality for other types.  Try this:

import arcpy from os import path as p arcpy.overwriteOutput = True   fc = r'F:\shape_clean_python\rioDej_fav.shp' field = 'FID' outws = r'F:\shape_clean_python\exports'   valList = [] with arcpy.da.SearchCursor(fc,[field]) as rows:     for row in rows:         value = row[0]         if value not in valList:             valList.append(value)    for val in valList:         for fld in arcpy.ListFields(fc):             if fld.name == field:                 if fld.type == 'String':                     query = '"{0}" = \'{1}\''.format(field,val)                 else:                     query = '"{0}" = {1}'.format(field,val)              output = p.join(outws,str(val).replace(' ','_'))         desc = arcpy.Describe(outws)         if desc.workspaceType == 'FileSystem':             output += '.shp'         if arcpy.Exists(output):             arcpy.Delete_management(output)         arcpy.Select_analysis(fc, output, query)         print 'Created "%s"' %p.basename(output)
0 Kudos
by Anonymous User
Not applicable
Actually this is better so that the field type test is not in the loop:

import arcpy
from os import path as p
arcpy.overwriteOutput = True


fc = r'F:\shape_clean_python\rioDej_fav.shp'
field = 'FID'
outws = r'F:\shape_clean_python\exports'


valList = []
with arcpy.da.SearchCursor(fc,[field]) as rows:
    for row in rows:
        value = row[0]
        if value not in valList:
            valList.append(value)
            
     for fld in arcpy.ListFields(fc):
        if fld.name == field:
            if fld.type == 'String':
                x = 'str'
            else:
                x = 'other'

    for val in valList:
        if x == 'str':
            query = '"{0}" = \'{1}\''.format(field,val)
        else:
            query = '"{0}" = {1}'.format(field,val)     
        output = p.join(outws,str(val).replace(' ','_'))
        desc = arcpy.Describe(outws)
        if desc.workspaceType == 'FileSystem':
            output += '.shp'
        if arcpy.Exists(output):
            arcpy.Delete_management(output)
        arcpy.Select_analysis(fc, output, query)
        print 'Created "%s"' %p.basename(output)
0 Kudos
ChrisBrannin
Occasional Contributor
Thank you, this worked like a charm! Greatly appreciated!
0 Kudos