Hi,I have used this script before in its original form and had a few dramas, but I have since updated it for our toolbox at work.Here is my code:## Created By: Cpl Barrett, GASC, 16 Geo Sp Sqn
## Date: 02/05/2013
## Purpose: Take and input feature class and split it based on the unique values
## in the supplied case field. If an ID field is used the result will be
## feature classes for each row in the input. The name of the input fc is
## prefixed to the output fc.
## note this has thrown an error for null values on a field this will be fixed in the next update.
import arcpy, string, os
def whereClause(fname, fc, i, unique):
"""Returns a formatted where clause for use in an SQL statement.
The format of the where clause is determined by the input field type.
"""
# get the field type from the supplied fname using it as a wildcard
field = arcpy.ListFields(fc, fname)
datatype = field[0].type
# construct the whereclause based on if the field is of type Date, Sring or a Number field
if datatype == "Date":
whereclause = """%(fname)s = date'%(value)s'""" % {"fname":arcpy.AddFieldDelimiters(fc, fname), "value": str(unique)}
elif datatype =="String":
whereclause = """%(fname)s = '%(value)s'""" % {"fname":arcpy.AddFieldDelimiters(fc, fname), "value": str(unique)}
else:
whereclause = """%(fname)s = %(value)s""" % {"fname":arcpy.AddFieldDelimiters(fc, fname), "value": str(unique)}
return whereclause
## Get the parameters and do the work
try:
# input feature class
fc = arcpy.GetParameterAsText(0)
# attribute field to search
field = arcpy.GetParameterAsText(1)
# output workspace
outworkspace = arcpy.GetParameterAsText(2)
# list to hold the values found in the attribut table
values = []
# search cursor used to return the field values
cursor = arcpy.SearchCursor(fc)
# determine the pefix for the output feature classes
prefix = (os.path.basename(fc)).rstrip(".shp")
print prefix
# get a list of each value in the attribute table
for row in cursor:
values.append(row.getValue(field))
# use set() and list() to get a list containing only the unique values
unique = list(set(values))
##for i in range(0, len(unique)):
## print unique
# Add a message that states how many unique values found in x number of rows
message = str(len(unique)) + " value(s) found in " + str(len(values)) + " rows."
arcpy.AddMessage(message)
# loop through each unique value and create a new fc
for i in range(0,len(unique)):
# get the whereclause
whereclause = whereClause(field, fc, i, unique)
# add a message to show what value is being extracted
message = "Extracting: " + str(unique)
arcpy.AddMessage(message)
# validate the name of the output feature class
name = arcpy.ValidateTableName((prefix + "_"+ str(unique)))
# make atemp layer to hold the selected values based on the where clause
layer = arcpy.MakeFeatureLayer_management(fc, name, whereclause)
outfc = outworkspace + os.sep + name
# create the output feature class
arcpy.CopyFeatures_management(layer, outfc)
arcpy.AddMessage(("Created: " + outfc))
except:
# Add a generic error message to let the user know something has broken
arcpy.AddError("Something has gone wrong. Please check the field values to ensure that there are no illegal characters.")
# Return Geoprocessing tool specific errors
#
for msg in range(0, arcpy.GetMessageCount()):
if arcpy.GetSeverity(msg) == 2:
arcpy.AddReturnMessage(msg)
This isn't perfect yet as it has an issue when fields contain null values. Haven't had a chance to alter it yet.Hope it helpsDave