Select to view content in your preferred language

calculate field but failed to paste string to the target field

1053
1
Jump to solution
05-28-2012 03:21 PM
ElaineKuo
Regular Contributor
System ArcGIS 9.3

Problem:
1. I have a shapefile of multiple polygons.
   The fields in this shapefiles start with C  (such as C7843, C1293, and C3498)
   Each C field has two string contents, S or W.

2. I want to select "S" (string) in all the fields of CXXXX.
   Then paste the string "S" to a newly-added field called "All".

3. The code below was run but  the fields of "All" remained empty.
Please kindly advise any modification to make the paste successful.
Thank you in advance.

 ##Script Name: calculate sum ##Description: calculate sum  ##Created By: Elaine Kuo ##Date: 26/05/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"  #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      # Add a field to this shapefile, of type LONG     gp.AddField (fc, "All", "Text", 6,6)         # Make temporary featureclasses     gp.MakeFeatureLayer(fc,"lyr")          #   Get a list of the fields in the featureclass     fields = gp.ListFields("lyr", "C*", "String")          # Loop through every item in the list that was just generated      for field in fields:          gp.toolbox = "Data Management"           # Select records to be copied (C*, i.e. C7658)         query = "\"%s\" = 'S'" % field.Name         gp.SelectLayerByAttribute("lyr", "ADD_TO_SELECTION", query)          # copy values in existing fields to the new field          gp.CalculateField("lyr", "All", 'S')               #Validate the new feature class name for the output workspace.     OutFeatureClass = outWorkspace + os.sep + gp.ValidateTableName(fc,outWorkspace)      # copy feature     gp.CopyFeatures("lyr", OutFeatureClass)      # clear memory of layers     gp.Delete("lyr")  gp.AddMessage(gp.GetMessages()) print gp.GetMessages()
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
FabianBlau
Deactivated User
Use print/AddMessage for testing. e.g print fcs and print fields to check if there are any featureclasses/fields found.

try this after SelectLayerByAttribute:
desc1 = gp.Describe("lyr") print desc1.FIDSet

is there anything selected?

Is this right:
gp.CalculateField("lyr", "All", 'S')

Try "'S'".

Check "ADD_TO_SELECTION":
gp.SelectLayerByAttribute("lyr", "ADD_TO_SELECTION", query)

This shoulc be no error, bur try either "New Selection" or gp.CalculateField("lyr", "All", 'S') outside the loop (fields).

View solution in original post

0 Kudos
1 Reply
FabianBlau
Deactivated User
Use print/AddMessage for testing. e.g print fcs and print fields to check if there are any featureclasses/fields found.

try this after SelectLayerByAttribute:
desc1 = gp.Describe("lyr") print desc1.FIDSet

is there anything selected?

Is this right:
gp.CalculateField("lyr", "All", 'S')

Try "'S'".

Check "ADD_TO_SELECTION":
gp.SelectLayerByAttribute("lyr", "ADD_TO_SELECTION", query)

This shoulc be no error, bur try either "New Selection" or gp.CalculateField("lyr", "All", 'S') outside the loop (fields).
0 Kudos