# Import the arcpy module import arcpy # Location of the feature class I want to update fc = "C:\\Data\\NothingSpecial.gdb\\calc" # Create a cursor using arcpy data access module cursor = arcpy.da.UpdateCursor(fc,"*") # For each row in the feature class: for row in cursor: print "For row: "+str(row) # row[2] is the idex position of the f1 column if row[2] <> None: x = row[2] print "x = %d" %(x) # row[3] is the idex position of the f2 column if row[3] <> None: y = row[3] print "y = %d" %(y) # row[4] is the idex position of the f3 column if row[4] <> None: z = row[4] print "z = %d" %(z) # Now use these values to calculate the "calc" column if x: calc = x print "calc = %d" %(x) if y: calc = calc + y print "calc + y = %d" %(calc) if z: calc = calc + z print "calc + z = %d" %(calc) # Set the calc column (row[5]) as the calculated value row[5] = calc # Update the row with the new value for "calc" cursor.updateRow(row) # Set all values back to 0 for the next row x=y=z=calc=0
Wanted to chime in here 10 years later to note that if you're using arcpro and you switch to an arcade expression (instead of python) in the field calculator it'll successfully read the nulls as zeros. Some classic esri nonsense when I have a deadline to meet, but glad I tried that and wanted to share here.
Thanks for the arcade tip- ignores all my null values and adds the fields that do have values- weird, but very helpful thanks!
Sum($feature.Jan,$feature.Feb,$feature.Mar,$feature.Apr,$feature.May,$feature.Jun,$feature.Jul,$feature.Aug,$feature.Sep,$feature.Oct,$feature.Nov,$feature.Decr)