7

I have been combing the help associated with (None) values in arc, and have not been able to solve the following problem:

I have a a field with mixed Null and integer values:

0 100

I would like to transform the null values to 0's in python:

 cursor = arcpy.UpdateCursor(fishJoin, ['myField']):
 for row in cursor:
 if row is None:
 row = 0
 cursor.updateRow(row)

This does not alter any of the null values.

I'm using ArcGIS Desktop 10.0.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Mar 11, 2014 at 20:49

1 Answer 1

13

I would recommend using the data access module da with the Update Cursor as you will notice significant performance improvements. The following is the correct syntax to replace <Null> (aka None) values with 0.

import arcpy
fc = r'C:\path\to\your\FGDB.gdb\andFC'
with arcpy.da.UpdateCursor(fc, ["Field_Name"]) as cursor:
 for row in cursor:
 if row[0] == None:
 row[0] = 0
 cursor.updateRow(row)
print "Processing complete"

The following is the correct syntax for ArcGIS 10.0 (i.e. without the da module).

import arcpy
fc = r'C:\path\to\your\FGDB.gdb\andFC'
cursor = arcpy.UpdateCursor(fc)
for row in cursor:
 if row.YourFieldName == None:
 row.YourFieldName = 0
 cursor.updateRow(row)
print "Processing complete"
del row
answered Mar 11, 2014 at 21:14
3
  • This script runs for me but does not end up changing the attribute. My attribute is not a <Null> value but rather empty (i.e. '') It prints "Processing Complete" but does not updated the value. Here is my code: import arcpy fc = r'D:\_data\sidewalk.gdb\repairs' cursor = arcpy.UpdateCursor(fc) for row in cursor: if row.FixedBy == None: row.YourFieldName = 'Contractor' cursor.updateRow(row) print "Processing complete" del row Commented Jun 8, 2017 at 13:31
  • @cbunn I would urge you to open a new question. It is difficult to understand your code in the comments. Commented Jun 8, 2017 at 13:37
  • Sorry, it is its own question now here: gis.stackexchange.com/questions/243256/… Commented Jun 8, 2017 at 13:39

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.