Saturday, 12 August 2017

arcgis 10.0 - Using ArcPy update cursor to replace value?


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.



Answer



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

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...