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")
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
Error:
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.
field
on the iteration the error occurs? – blah238 Apr 2 '13 at 17:50