Select to view content in your preferred language

query issues with Update Cursor

1021
2
Jump to solution
09-27-2012 01:00 PM
stephengushue
Deactivated User
Hello All,

I am working on an update cursor on a contour line feature class. The goal is to update a field "INDEX_VALU" with the string "10 foot Index" where data in the "CONTOUR" field are divisible by 10. In tests I was success when setting a simple query as shown:

query = ' "CONTOUR" = 5100'


The issues occurs when I try to modify this query to include all values divisible by 10. I believe the modulo operator is the way forward, but they query keeps getting rejected. Enclosed is my latest attempt at solving this. Any help will be appreciated.

# Script Name: contourindexcalc.py # Author: Stephen Gushue # Purpose: Find and calculate values of the contours to illustrate 10 foot contours  # Import system modules import arcpy, sys, traceback from arcpy import env  # Set workspace arcpy.env.workspace = 'U:\\GEO\\projects\\python'  # Local Variables featureclass = 'contours_test.shp' fieldlist = arcpy.ListFields (featureclass)  try:     # Sets row hold query     setfield = '"CONTOUR"%'     query = 'setfield == 0'          # Create a Search Cursor     srows = arcpy.SearchCursor (featureclass, query)      for srow in srows:          # assign a variable for the value of srow.CONTOUR         # assign a variable for the value of srow.INDEX_VALU         contour = srow.CONTOUR         index = srow.INDEX_VALU                    # Create Update Cursor         urows = arcpy.UpdateCursor(featureclass, query)          # Cycle through the rows         # to actually update the row in the cursor         # with the values obtained from the search cursor         for urow in urows:             urow.INDEX_VALU = "10 Foot Index"             urows.updateRow(urow)      print 'DONE'     del urow, urows, srow, srows  except:      tb = sys.exc_info()[2]     tbinfo = traceback.format_tb(tb)[0]     pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n"     msgs = "ARCPY ERRORS:\n" + arcpy.GetMessages(2) + "\n"      arcpy.AddError(msgs)     arcpy.AddError(pymsg)      print msgs     print pymsg
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Honored Contributor
I think you need to start at square one here. There is no reason to use a search cursor and an update cursor at the same time on the same feature class.

This should be all you need in your try statement.
try:     # Create Update Cursor     urows = arcpy.UpdateCursor(featureclass)      # Cycle through the rows     # to actually update the row in the cursor     # with the values obtained from the search cursor     for urow in urows:         contour = urow.CONTOUR         if int(float(contour) / 10) == (float(contour) / 10):             urow.INDEX_VALU = "10 Foot Index"         urows.updateRow(urow)      print 'DONE'     del urow, urows

View solution in original post

0 Kudos
2 Replies
MathewCoyle
Honored Contributor
I think you need to start at square one here. There is no reason to use a search cursor and an update cursor at the same time on the same feature class.

This should be all you need in your try statement.
try:     # Create Update Cursor     urows = arcpy.UpdateCursor(featureclass)      # Cycle through the rows     # to actually update the row in the cursor     # with the values obtained from the search cursor     for urow in urows:         contour = urow.CONTOUR         if int(float(contour) / 10) == (float(contour) / 10):             urow.INDEX_VALU = "10 Foot Index"         urows.updateRow(urow)      print 'DONE'     del urow, urows
0 Kudos
stephengushue
Deactivated User
Matthew,

Thank you. I realize I am stepping all over myself trying to learn and implement Python into my workflow. I appreciate the assistance..
0 Kudos