Select to view content in your preferred language

PYT Split by Attribute Tool

1377
4
Jump to solution
05-24-2013 08:27 AM
JohnDye
Deactivated User
Any ideas why I'm getting this error:

Traceback (most recent call last):
File "<string>", line 90, in execute
File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\analysis.py", line 98, in Select
raise e
ExecuteError: ERROR 000210: Cannot create output C:\Users\jdk588\Documents\New File Geodatabase.gdb\Cafe_27242.0
ERROR 000361: The name starts with an invalid character
Failed to execute (Select).

Failed to execute (Split).

From this tool:
class Toolbox(object):
    def __init__(self):
        """Define the toolbox (the name of the toolbox is the name of the
        .pyt file)."""
        self.label = "COF"
        self.alias = "cof"

        # List of tool classes associated with this toolbox
        self.tools = [Split]

class Split(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Split by Unique Value"
        self.description = "Splits an input featureclass into multiple feature classes by a unique value."
        self.canRunInBackground = True

    def getParameterInfo(self):
        """Define workspace parameter"""
        workspace = arcpy.Parameter(
            displayName = "Workspace",
            name = "in_workspace",
            datatype = "DEWorkspace",
            parameterType = "Required",
            direction = "Input",)

        """Apply workspace parameter filter"""
        workspace.filter.list= ["Local Database"]

        """Define in_featureclass parameter"""
        in_featureclass = arcpy.Parameter(
            displayName = "Feature Class to Split",
            name = "in_fc",
            datatype = "Feature Layer",
            parameterType = "Required",
            direction = "Input")

        """Apply a Geometry Type Filter to in_featureclass"""
        in_featureclass.filter.list = ["Point", "Polygon", "Polyline", "Multipoint"]

        """Define unique field parameter"""
        unique_field = arcpy.Parameter(
            displayName = "Field containing Unique Values",
            name = "unique_values",
            datatype = "Field",
            parameterType = "Required",
            direction = "Input")

        """Apply a dependency to unique field parameter"""
        unique_field.parameterDependencies = [in_featureclass.name]

        """Define outfc naming prefix parameter"""
        pfx = arcpy.Parameter(
            displayName = "Output Prefix",
            name = "out_pfx",
            datatype = "GPString",
            parameterType = "Required",
            direction = "Input")

        params = [workspace, in_featureclass, unique_field, pfx]
        return params

    def isLicensed(self):
        return True

    def updateParameters(self, parameters):
        return

    def updateMessages(self, parameters):
        return

    def execute(self, parameters, messages):
        """The source code of the tool."""
        arcpy.env.workspace = parameters[0].valueAsText
        fc = parameters[1].valueAsText
        field = parameters[2].valueAsText
        name = parameters[3].valueAsText

        uniqueSet = set([r[0] for r in arcpy.da.SearchCursor (fc, [field])])

        for uniqueVal in uniqueSet:
            prefix = name + "_" + str(uniqueVal)
            arcpy.AddMessage("Splitting " + str(uniqueVal) + "...")
            arcpy.Select_analysis(fc, str(prefix), field + " = " + str(uniqueVal))
            arcpy.AddMessage("Success.")
        arcpy.AddMessage("All Unique Values successfully exported.")
 return


It's obvious I'm missing something, but I don't know what.
The value being entered in the pfx is just "Cafe" and it is taken as a GPString under the parameter and then converted to string again at 'name'. So...what gives? Why is the name invalid? And one more thing...why is it dropping that '.0" at the end of the name? The value its pulling from the field is '27240', where is the '.0' coming from??
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Alum
  Why is the name invalid? And one more thing...why is it dropping that '.0" at the end of the name? The value its pulling from the field is '27240', where is the '.0' coming from??


The name is invalid because names in the gdb cannot contain ".".

The ".0" is appearing I would guess because if the input field is float or double, the string representation in Python includes the ".0".

View solution in original post

0 Kudos
4 Replies
curtvprice
MVP Alum
  Why is the name invalid? And one more thing...why is it dropping that '.0" at the end of the name? The value its pulling from the field is '27240', where is the '.0' coming from??


The name is invalid because names in the gdb cannot contain ".".

The ".0" is appearing I would guess because if the input field is float or double, the string representation in Python includes the ".0".
0 Kudos
JohnDye
Deactivated User
Thanks Curt!
That was it, except now it's saying my expression is invalid.

ExecuteError: ERROR 000358: Invalid expression DID = 27242
Failed to execute (Select).

Can you spot anything wrong with it?
0 Kudos
JohnDye
Deactivated User
Thanks Curt! 
That was it, except now it's saying my expression is invalid. 

ExecuteError: ERROR 000358: Invalid expression DID = 27242
Failed to execute (Select).


Can you spot anything wrong with it?


Nevermind, got it. strings require quotes...

arcpy.Select_analysis(fc, str(prefix), "'" + field + "' = " + "'" + str(uniqueVal) + "'")


Thanks again curt!
0 Kudos
JohnDye
Deactivated User
The name is invalid because names in the gdb cannot contain ".".

The ".0" is appearing I would guess because if the input field is float or double, the string representation in Python includes the ".0".


Spot on. Converted the field to string and after fiddling with my expression, it worked perfectly. Thanks curt!

Guess I'll go back and apply a string filter to the field.
0 Kudos