I'm attempting to re-position a question I asked yesterday here. I am starting to think that the way I wrote my code just isn't the best way. Am I overcomplicating things?

I know for a fact that adding a bunch of new fields and then calculating those fields based on the new fields is a common occurrence in GIS, so I just can't understand why it's so difficult to find out how to do it in a Python script. The following code is a snipet of what I came up with, but despite hours and hours of troubleshooting, I keep getting an error (although it did work on a small sample size). I'll paste the code first, and then the error below.

I'm receiving the error just after all of the fields are successfully added. I've done a number of things in an effort to troubleshoot, such as import all of my files into a geodatabase and run it from there; I've tested small samples of data (which does actually work, so I know the code "works"), and I've switched out fields I thought were problematic with other fields that were not. So far, I'm unable to run this code on all 108 fields that I'm trying to add and then calculate. Again, I receive an error just before it attempts to calculate the fields, and it seems to be referring to the line: value = row.getValue(field)* row.anotherField

Any suggestions for a better way to do this??

Code snipet:


fc = arcpy.CopyFeatures_management(layerName, outFeatures)

# Add fields to a feature class:
fieldList = ["field1", "field2", "field3", ............., "field99"]  #<-- fieldList
fieldListx = []
for f in fieldList:
    fieldx = f+"x"
    arcpy.AddField_management(fc, fieldx, "DOUBLE")

# Calculate fields:
rows = arcpy.UpdateCursor(fc)
for row in rows:
    for fieldx in fieldListx:
        field = fieldx.rstrip('x')
        value = row.getValue(field)* row.anotherField  #<--This is the line the error is referring to
del row, rows


 Traceback (most recent call last):
   File "C:\GIS\1GIS_DATA\CUR_GIS\Election_Districts\Python and Tools\Join_Voter_Results_to_Allocation_Layer_and_Add_New_Fields.py", line 125, in <module>
     value = row.getValue(field)* row.UNpctOfEd #<--STOPS WORKING WHEN I ADD THE LARGER LIST!!!
   File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\arcobjects\arcobjects.py", line 944, in getValue
     return convertArcObjectToPythonObject(self._arc_object.GetValue(*gp_fixargs(args)))
 RuntimeError: ERROR 999999: Error executing function.
  • 1
    What type of field is field on the iteration the error occurs?
    – blah238
    Apr 2 '13 at 17:50
  • The field type is double. (I change some of the code, and it seems to be working. I'll post an answer soon if it does.)
    – Kristen G.
    Apr 2 '13 at 17:59

With respect to the line throwing the error: value = row.getValue(field)* row.UNpctOfEd maybe check the data type or value in each of the f and UNpctOfEd fields? Check note 1 below.

If all input fields are similarly named, you might also want to generate your fieldList with the List Fields function instead of typing it out:

# list fields for calculations
wild_card = "field*"
listFields = arcpy.ListFields (fc, wild_card, "DOUBLE")

Use the field_type parameter if you know your fields of interest are only of one data type - in this example "DOUBLE" (if not all the same then leave this out; if a given field is not numeric then see note 1 below).

Unless I'm missing something here, you shouldn't need to perform the field calculation inside a cursor (unless perhaps you need to accumulate values or similar) nor go through a second loop. Why not use the Calculate Field tool directly inside the first loop?

# loop through fieldList    
for f in fieldList:
    # local variables
    fieldx = f + "x"
    expression = "!" + f + "! * !" + anotherField + "!"
    # add/calculate new field
    arcpy.AddField_management(fc, fieldx, "DOUBLE")
    arcpy.CalculateField_management(fc, fieldx, expression, "PYTHON_9.3")


  1. If you know that your values may be stored as string then you will need to cast as numeric; e.g. replace f with float(f) in the expression variable. This is related to the comment from blah238.
  • +1 except your second note is not correct; the field delimiters used in Field Calculator expressions are not the same as those added by AddFieldDelimiters, which is only concerned with SQL syntax.
    – blah238
    Apr 2 '13 at 20:35
  • Incredible! This is exactly what I was looking for. Simplification. The only thing I had to do was to add quotes around anotherField since in my case that isn't a variable, but a pre-existing field. So instead of what you have, I used expression = "!" + f + "! * !" + "anotherField" + "!", and it worked!
    – Kristen G.
    Apr 2 '13 at 21:30
  • 1
    If not a variable then you can simplify more: expression = "!" + f + "! * !anotherField!"
    – ccn
    Apr 3 '13 at 4:41
  • Oops, @blah238 is right about the AddFieldDelimiters so removed that from the notes.
    – ccn
    Apr 3 '13 at 4:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.