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

migration rejected from stackoverflow.com Nov 10 at 12:11

This question came from our site for professional and enthusiast programmers. Votes, comments, and answers are locked due to the question being closed here, but it may be eligible for editing and reopening on the site where it originated.

closed as off-topic by Fezter, user30184, AndreJ, BradHards, Erica Nov 10 at 12:11

This question appears to be off-topic. The users who voted to close gave these specific reasons:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Fezter, user30184, Erica
  • "Questions about software development are off-topic here unless they relate directly to Geographical Information Systems, but they can be asked on Stack Overflow." – AndreJ, BradHards
If this question can be reworded to fit the rules in the help center, please edit the question.

1  
Please consider adding your solution as an answer so we can consider this question answered. –  Aaron Nov 9 at 23:58