AnsweredAssumed Answered

Python UpdateCursor & CalculateField working together

Question asked by Cep86 on Jul 31, 2015
Latest reply on Aug 10, 2015 by Cep86

I have a script that I recently created an alternate copy of and updated many of the calculate field functions to make use of UpdateCursor.  When I run the original script, all runs through just fine, but whenever I try to run the alternate version, the tool gets hung up on any of the parts that are still in CalculateField format.  Is there a known issue with mixing UpdateCursors and CalculateField functions within the same script?  Is there a common procedure that needs to be followed to do so?  Sample code below. 

 

Other than changing from CalculateField to UpdateCursor, the only other difference that I see that I made was moving some of the variables into the functions as global variables so I could use them outside of the function (I think I had to anyway...these sections started working after I did so).

 

#########################
# Calculate Region
#########################

# Start message
arcpy.AddMessage("Determining the correct region field to be updated for all node features...")

# Set local variables
inTable = nodeFeatures
cursor = arcpy.UpdateCursor(inTable)

# Determine the correct region field to update
def Get_node_regionField():
    global node_regionField
    if carrier == "CUST1":
        node_regionField = "CUST1Region"
    elif carrier == "CUST2":
        node_regionField = "CUST2Region"
    elif carrier == "CUST3":
        node_regionField = "CUST3Region"
    elif carrier == "CUST4":
        node_regionField = "CUST4Region"
    elif carrier == "CUST5":
        node_regionField = "CUST5Region"
    else:
        pass
Get_node_regionField()

# Execute UpdateCursor
for row in cursor:
    row.setValue(node_regionField, region)
    cursor.updateRow(row)

# End message
arcpy.AddMessage("The " + carrier + " region field has been updated for all node features!")
arcpy.AddMessage("...")
arcpy.AddMessage("...")

 

Each one of these IF statements used to be its own CalculateField (I didn't know how to incorporate them all into one, as I do above using the UpdateCursor method).  The above code works just fine.  I originally had the "node_regionField" variable as "inField", but there was an issue with doing so since I used inField for multiple other UpdateCursors.  I'm assuming it had something to do with the fact that it is a global variable...?  Not sure.  Comment on that if you know, but the real issue is below:

 

#########################
# Calulate latitude
#########################

# Start message
arcpy.AddMessage("Calculating the latitude values for all node features...")

# Set local variables
inTable = nodeFeatures
inField = "Latitude"
expression = "!Shape.Centroid.Y!"

# Execute CalculateField
arcpy.CalculateField_management(inTable, inField, expression, "PYTHON_9.3")


# End message
arcpy.AddMessage("The latitude values have been calculated for all node features!")
arcpy.AddMessage("...")
arcpy.AddMessage("...")

 

This code will not run, and all it does is return a generic 99999 error code:

"Traceback (most recent call last):

  #my python script filepath#

    arcpy.CalculateField_management(inTable, inField, expression, "PYTHON_9.3")

  File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\management.py", line 3354, in CalculateField

    raise e

ExecuteError: ERROR 999999: Error executing function.

Failed to execute (CalculateField)."

 

This error code doesn't really tell me much.  The weirdest thing is that it still works (copied identically) in the script that utilizes all CalculateField functions.  I have 2 sections that calculate longitude, 2 that calculate latitude, 1 that calculates feet, 1 for meters, and 1 for miles.  All 7 of them are constructed the same, and all 7 generate the same error message while in the mixed script, and all 7 work just fine in the all CalculateField version.

 

Side note...if the !shape.centroid@y! can be done using the UpdateCursor method (I couldn't figure out how), I would much rather that anyway.

Outcomes