Select to view content in your preferred language

If statement won't bypass zeros

924
6
11-23-2011 11:02 AM
RichardThurau
Deactivated User
Hello,
Trying to calculate fields using some division. Of course, division by zero is bad. Created what I thought was a simple if statement to skip zeros:

rows2 = arcpy.SearchCursor(parcels)
for row2 in rows2:
    land_area = row2.getValue("Land_Area")
    if land_area > 0:
        arcpy.CalculateField_management(parcels, "UTC10_Pct", """[UTC10_Ft]/[Land_Area] """, "VB")


Overflow Error

I know I can get around this using a selection, but seems like should simply run from Python.
Can anyone tell why this isn't working?

Thanks
Rich
Tags (2)
0 Kudos
6 Replies
MathewCoyle
Honored Contributor
As far as I know, the calculate field tool processes on the entire field, not on your current search cursor selection. I could be wrong.

Why not use update cursor? Much better than calculate field. And why not use a selection instead of a search cursor?
0 Kudos
RichardThurau
Deactivated User
Mathew,
Thanks, that makes sense. I'm still gaining comfort with moving into python away from the tools I know. I will try incorporating the update cursor.

I have run the tool using the selection successfully.

Thanks a bunch for your feedback.

Rich
0 Kudos
DanPatterson_Retired
MVP Emeritus
Calculate field is used on the whole field, use an updatecursor as suggested, then put in an else section telling what to do if the divisor is 0
0 Kudos
RichardThurau
Deactivated User
Dan Thanks.
What I have been reading suggests an If statement can be singular without an else statement. Perhaps your experience is otherwise.

RT
0 Kudos
MathewCoyle
Honored Contributor
I would recommend something as follows. You could use the getValue instead of row references too. Format where statement as needed.

where = "\"Land_Area\" > 0"
rows2 = arcpy.UpdateCursor(parcels, where)
for row2 in rows2:
    row.UTC10_Pct = row.UTC10_Ft / row.Land_Area
    rows2.updateRow(row2)
0 Kudos
RichardThurau
Deactivated User
Works awesome. Thanks
0 Kudos