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 my first post, so any help is greatly appreciated. I'm trying to use an update cursor to calculate a field. Basically, I'm trying to avoid division by 0 by looking in row[0] and see if it's 0. My code thus far is:

import arcpy
fc="C:/Temp/crimeGrid.shp"
fields=('MEAN_GRIDC', 'SUM_Join_C', 'crimeRate')
with arcpy.da.UpdateCursor(fc,fields) as cursor:
    for row in cursor:
        if (row[0] > 0):
            row[2] = row[1]/row[0]
        else:
            row[2] = 0
        cursor.updateRow(row)

Any ideas or help on what I'm missing? I can only assume (yes I know what assuming does) that it's the calculation aspect of my code. Thanks!!!

UPDATE: Sorry for the ridiculous question. I continually thought it was in the division of rows, so I looked into my fields a little closer. Turns out, Row[0] and Row[1] were Double and Row[2] was Long. Fixed that aspect and the code works as is.

share|improve this question

migrated from stackoverflow.com Dec 26 '13 at 3:33

This question came from our site for professional and enthusiast programmers.

1 Answer 1

Your 'fields' is delimited with a double quote in the beginning and a bracket in the end...

share|improve this answer
    
That was my mistake when entering the code. It should have been ( as opposed to ". Even correcting this doesn't yield the results expected. The 'crimeRate' field remains 0 across the board. I then tried to change the fields variable to contain a list using [] as opposed to (). Still no luck. –  user3078127 Dec 7 '13 at 22:09

Your Answer

 
discard

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