Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

This is the bottom half of my code (and it's been edited somewhat to share publicly). The code works perfectly fine when the fieldList has only three fields in it, but when I populate it with all 108 fields, I receive the following error, just before it starts to calculate fields:

Traceback (most recent call last):
  File "C:\GIS\1GIS_DATA\Folder1\Folder2\Python and Tools\RunThisPythonCode.py", line 125, in <module>
    value = row.getValue(field)* row.anotherField #<--This is the line the error is referring to
  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.

Here's the code:

....

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")
    fieldListx.append(fieldx)

#---------------------------------------------
# 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
        row.setValue(fieldx,value)
    rows.updateRow(row)
del row, rows

Is this an issue of memory or something? Why does it produce an error before Calculate Fields when I add the full list of 108 fields, but not when I run a small sample of only 3? Thanks!!

share|improve this question
    
G, Is your code erroring at the same field calculation each time (for the row.anotherfield? Is row.anotherfield type non text? –  artwork21 Apr 2 '13 at 11:44
    
Yeah, it errors during the 1st iteration, every time. –  Kristen G. Apr 2 '13 at 13:42
    
Non-text. Hmm... Yea it's a double. But the weird thing is the whole code works fine on a sample set of three fields, but not on the full set of 108 fields. –  Kristen G. Apr 2 '13 at 13:43
1  
Did you try a number of fields between 3 and 108? Might be useful to know at what number of fields it first breaks down. At a minimum, print out the field number on each iteration so we know what field number if chokes on. –  Llaves Apr 2 '13 at 13:50
1  
So that begs the question, what's up with that 11th field? Look into that. –  Chad Cooper Apr 2 '13 at 15:20
show 9 more comments

1 Answer

up vote 2 down vote accepted

OK, there are two solutions to the above. I will paste them both below... Thanks everyone for all of your help!

Ultimately the problem WAS with a field name. My field#11 had at some point lost its last letter. When it was being appended to fieldList and later asked to "strip" itself of the "x" that had been added to it, it was looking for a field in the feature class that didn't exist. Despite the fact that locating that problem was like finding a needle in a haystack, I fixed it, and I didn't receive the error again. Lesson learned: always check your field names!!

Below, however, are "solutions" simply to writing better code to do what I was trying to do above:

Solution #1:
This one is the best. It requires fewer lines and runs faster. It is the result of a suggestion from @ccn in this post. I tweaked it just slightly by including quotations around the static field name UNpctOfEd.

....

fc = arcpy.CopyFeatures_management(layerName, outFeatures)

fieldList = ["field1", "field2", "field3", ............., "field105"]  #<-- this has to be done manually for various reasons so I can't just use ListFields

# Loop through fieldList (Add and Calculate fields)
for field in fieldList:
    # local variables
    fieldx = field + "x"
    expression = "!" + field + "! * !" + "UNpctOfEd" + "!" #<-- Jackpot!! Figuring out how to write the expression
    # add/calculate new field
    arcpy.AddField_management(fc, fieldx, "DOUBLE")
    arcpy.CalculateField_management(fc, fieldx, expression, "PYTHON_9.3")

Solution #2:
This one is a working/much better version of what I had in the question above, using UpdateCursor although it requires more lines, is more complicated, and is slower than Solution #1:

....

fc = arcpy.CopyFeatures_management(layerName, outFeatures)

fieldList = ["field1", "field2", "field3", ............., "field105"]  #<-- again, this has to be done manually for various reasons so I can't just use ListFields

# Creating new fields in the feature class
fieldListx = []
for f in fieldList:
    fieldx = f+"x"
    arcpy.AddField_management(fc, fieldx, "DOUBLE")
    fieldListx.append(fieldx)
print fieldListx

# Calculating those new fields in the feature class
for fieldx in fieldListx:
    rows = arcpy.UpdateCursor(fc)
    field = fieldx.rstrip('x')
    for row in rows:
        value = row.getValue(field)* row.UNpctOfEd 
        row.setValue(fieldx,value)
        rows.updateRow(row)
    del row, rows
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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