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 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.

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: 
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
        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

             field_value= 100000.0* (float(c1)/float(p1))
    except:
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
###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"
share|improve this question
    
Is your data numerical? If "Cnt1" or "Pop1" is a string, the if true field_value calculation will fail and the except clause will give you field_value = 0. –  gm70560 Apr 9 '13 at 14:10
    
This question should be merged with the one referenced by @blord-castillo in the comment above. Duplicate posting of questions really makes it difficult to track down the ultimate solution to the root question being asked. –  Jason Apr 9 '13 at 16:30
    
how do you merge questions? –  David Meek Apr 9 '13 at 17:43
add comment

marked as duplicate by blord-castillo, Mapperz Apr 9 '13 at 16:55

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

I see you're still having trouble with this question. In this scenario you want to replace field_value = 100000.0* (c1/p1) with field_value = 100000.0* (float(c1)/float(p1)) to force floating point math.

EDIT: Okay, I think I see your issue. When you create your cursor object you are naming it "cursor". When you call it's updateRow method you are calling rows.updateRow() when you should be calling cursor.updateRow(). Pick one name or the other, either "rows" or "cursor", and stick with it and you should be good to go.

share|improve this answer
    
unfortunately that doesn't work either. Still all 0s. –  David Meek Apr 9 '13 at 1:14
    
Edited my answer, see above. –  Jason Apr 9 '13 at 12:25
1  
Yep, that was a mistake in the code I typed in the original problem. I changed that in the answer on the original question. –  blord-castillo Apr 9 '13 at 15:17
    
unfortunately, still no dice. I updated the code on the original question with the cursor. but still no luck. I've also removed the parameters for the precision and scale in hopes going default would solve it; lastly, have put in a few things to try to force float. No luck on any front. –  David Meek Apr 9 '13 at 17:45
add comment

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