This question already has an answer here:
I'm trying to take a field calculator equation and do it in arcpy directly through IDLE, but am running into a problem where the process runs correctly, but the results in the field show up as 0. I've tried a variety of ways of trouble shooting this, including a massive amount of time and energy trying it in field calculator itself, and playing around with the field types , but not luck so far. Any constructive advice would be much appreciated.
note I've tried changing this field_value=0 part to 7 to see if it populates the field with 7 but it doesn't
import arcpy, sys arcpy.env.workspace = "C:\Temp" shapefile = "USCancer2000.shp" field_name = "Check8" print "start your engines" #Add the field arcpy.AddField_management(shapefile,field_name,"FLOAT", "5","5","","","NULLABLE","NON_REQUIRED","") cursor = arcpy.UpdateCursor(shapefile) for row in cursor: #Get the value of each of the fields, and put them into variables c1 = row.getValue("Cnt1") c2 = row.getValue("Cnt2") c3 = row.getValue("Cnt3") p1 = row.getValue("Pop1") p2 = row.getValue("Pop2") p3 = row.getValue("Pop3") #check for missing values, assuming that non-missing values are all greater than or equal to zero try:
if((min(c1,c2,c3,p1,p2,p3) < 0)): field_value = 0 else:
have tried putting in float in both cases, and the .0 attached to the 100,000 to try to #force float
note I've tried changing this field_value=0 part to 8 to see if it populates the field with 8 but it doesn't
field_value= 100000.0* (float(c1)/float(p1)) except:
###If something goes wrong, set the value to 0 still field_value = 0 #Set the value of field for just this row; note have tried inserting the cursor. #name for both ".setValue", and for just .updateRow, and also just for one. Still #doesn't work cursor.setValue(field_name,field_value) cursor.updateRow(row) del row del cursor print "done"