Select to view content in your preferred language

how to run a python tbx (split row by attributes)

3286
2
Jump to solution
09-21-2012 05:01 PM
by Anonymous User
Not applicable
Original User: Helenak

System: ArcGIS 9.3
Window Vista

Problem:

I wrote a python code to run the split by attributes tbx attached.
However, an error jumped out, saying
AttributeError: Object: Tool or environment <SplitLayerByAttributes> not found.

Please kindly comment on the code after having a try of the attachments.
(shapefile: test1/tbx: Axxxxx.zip)

Thank you.

##Script Name: split rows ##Description: using  ##Created By: Elaine Kuo ##Date: 24/09/2012   #Import standard library modules import arcgisscripting import os  #Create the Geoprocessor object gp = arcgisscripting.create(9.3)   #Set the workspace. gp.Workspace= "H:/temp/test1"   #Set the workspace. List all of the feature classes in the dataset outWorkspace= "H:/temp/test1"  # Add a toolbox with a model to the geoprocessor and set the workspace #gp.AddToolbox("H:/python_p/SplitLayerByAttributes.tbx")   #Get a list of the featureclasses in the input folder fcs = gp.ListFeatureClasses()  # Loop through every item in the list that was just generated for fc in fcs:      # Break out the name, no path or extension, using the describe object.     desc = gp.describe(fc)     featureName = desc.name            #Validate the new feature class name for the output workspace.     OutFeatureClass = outWorkspace + os.sep + gp.ValidateTableName(fc,outWorkspace)      #get file name     fcName, fcExt = os.path.splitext(fc)       # replace the strings you want to     fcName = fcName.replace("_Dissolve","")      #build field name     fieldName = fcName      fcName = fcName + fcExt      # rename the files     gp.Rename_management(fc, fcName)        #build field name     fieldName = fcName      #### Execute Script Tool ####     gp.SplitLayerByAttributes(fc, fieldName, OutFeatureClass, outWorkspace)     gp.AddMessage(gp.GetMessages()) print gp.GetMessages()                            
0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus
see your other post

View solution in original post

0 Kudos
2 Replies
DanPatterson_Retired
MVP Emeritus
see your other post
0 Kudos
by Anonymous User
Not applicable
Original User: Helenak

Thanks.
I made my own code.

##Script Name: split rows
##Description: to split featureclasses based on attributes
##Created By: Elaine Kuo
##Date: 24/09/2012


#Import standard library modules
import arcgisscripting
import os

#Create the Geoprocessor object
gp = arcgisscripting.create(9.3)


#Set the workspace.
gp.Workspace= "H:/temp/test"

#Set the workspace. List all of the feature classes in the dataset
outWorkspace= "H:/temp/test"

#-----------------------------------------------------------------
#Get a list of the featureclasses in the input folder
fcs = gp.ListFeatureClasses()

# Loop through every item in the list that was just generated
for fc in fcs:

    # Break out the name, no path or extension, using the describe object.
    desc = gp.describe(fc)
    featureName = desc.name
    
    #   Get a list of the fields in the featureclass
    fields = gp.listFields(fc, "C*", "String")

#Select S 
#-----------------------------------------------------------------    
    
    # Loop through every item in the list that was just generated 
    for field in fields:

        gp.toolbox = "Data Management"
        gp.addMessage(type(field))

        # Select records (C*, i.e. C7658)
        # Make temporary featureclasses
        gp.toolbox = "Data Management"
        query = "\"%s\" = 'S'" % field.Name
    
        gp.select_analysis(fc,("outputS.shp"),query)

        #get file name
        fcName, fcExt = os.path.splitext(fc) 

        # replace the strings you want to
        fcName = fcName.replace("_Dissolve","S")
        fcName = fcName + fcExt

        gp.rename_management("outputS.shp", fcName)
        gp.delete(fc) 

#-----------------------------------------------------------------
#-----------------------------------------------------------------
        #get file name
        #fcName, fcExt = os.path.splitext(fc) 

        # replace the strings you want to
        #fcName = fcName.replace("_Dissolve"," ")
        #fcName = fcName + fcExt
        #gp.rename_management(fc, fcName)
      

        # clear memory of temporary shapefiles
        #gp.delete("outputS.shp")  
        

gp.AddMessage(gp.GetMessages())
print gp.GetMessages()

        
   

            
0 Kudos