# Import modules and create the geoprocessor object
import arcgisscripting, sys, os
gp = arcgisscripting.create()
gp.workspace = os.getcwd()
# File to process
shp = "File1.shp"
# Set total to zero and start a blank list
total=0
keepTrack=[]
# Create update cursor for feature class.
rows = gp.UpdateCursor(shp)
row = rows.Next()
while row:
# Get the values from the fields
id = row.GetValue("ID")
len = row.GetValue("LENGTH")
# If id is unchanged
if id in keepTrack:
# Add length to the total
total = total + len
# If id changes
if not id in keepTrack:
# Total starts over as length value
total = len
# Clear the id list
keepTrack=[]
# Keep track of the id
keepTrack.append(id)
print "id: "+str(id), "length: "+str(len), "total: "+str(total)
# Write the total to field "CUM_LENGTH"
row.CUM_LENGTH = total
# Execute the new value to the table
rows.UpdateRow(row)
# Go to the next
row = rows.Next()
del rows
Here's the code with the proper indenting (I finally figured out how to post code properly on this new site).# Import modules and create the geoprocessor object import arcgisscripting, sys, os gp = arcgisscripting.create() gp.workspace = os.getcwd() # File to process shp = "File1.shp" # Set total to zero and start a blank list total=0 keepTrack=[] # Create update cursor for feature class. rows = gp.UpdateCursor(shp) row = rows.Next() while row: # Get the values from the fields id = row.GetValue("ID") len = row.GetValue("LENGTH") # If id is unchanged if id in keepTrack: # Add length to the total total = total + len # If id changes if not id in keepTrack: # Total starts over as length value total = len # Clear the id list keepTrack=[] # Keep track of the id keepTrack.append(id) print "id: "+str(id), "length: "+str(len), "total: "+str(total) # Write the total to field "CUM_LENGTH" row.CUM_LENGTH = total # Execute the new value to the table rows.UpdateRow(row) # Go to the next row = rows.Next() del rows
# Import modules and create the geoprocessor object
import arcgisscripting, sys, os
gp = arcgisscripting.create()
#gp.workspace = os.getcwd()
gp.workspace = sys.argv[1]
# File to process
shp = sys.argv[2]
# Set total to zero and start a blank list
total=0
keepTrack=[]
# Create update cursor for feature class.
rows = gp.UpdateCursor(shp)
row = rows.Next()
while row:
# Get the values from the fields
id = row.GetValue("ID")
len = row.GetValue("LENGTH")
# If id is unchanged
if id in keepTrack:
# Add length to the total
total = total + len
# If id changes
if not id in keepTrack:
# Total starts over as length value
total = len
# Clear the id list
keepTrack=[]
# Keep track of the id
keepTrack.append(id)
print "id: "+str(id), "length: "+str(len), "total: "+str(total)
# Write the total to field "CUM_LENGTH"
row.CUM_LENGTH = total
# Execute the new value to the table
rows.UpdateRow(row)
# Go to the next
row = rows.Next()
del rows