I am trying to replace NULL values for -999 in a table inside a gdb. I know how to replace the value for individual fields, but get an error when looping through the complete table.
import arcpy
fc ="C:\gSSURGO.gdb\chorizon_va_1"
fieldList = arcpy.ListFields(fc)
with arcpy.UpdateCursor(fc, [fieldList]) as cursor:
fRange = range(len(fieldList))
for row in cursor:
for index in fRange:
if row[index] == None:
row[index] = -999
cursor.updateRow(row)
And this is the error I get:
Runtime error : 'Cursor' object has no attribute '_exit_'
I found some examples using arcpy.da.UpdateCursor, but when I use that I get this error:
Runtime error <type 'exceptions.AttributeError'>: 'module' object has no attribute 'da'
I am using ArcMap 10.0.
-
What version of ArcGIS for Desktop are you using?PolyGeo– PolyGeo ♦2015年07月18日 19:57:28 +00:00Commented Jul 18, 2015 at 19:57
-
I am using ArcMap 10.0Iced_– Iced_2015年07月18日 20:01:55 +00:00Commented Jul 18, 2015 at 20:01
-
Just as an aside, I generally find None (NULL) is much more suited to representing absence than a value like -999. Although you haven't elaborated on your reasons, consider that NULL is far less ambiguous than a numerical value, especially if down the track someone other than you will be using the data. In some cases, -999 is s meaningful value in itself. Further, it is easier for someone to filter out None values than to have to guess what value represents NULL and then filter them.alphabetasoup– alphabetasoup2015年07月19日 04:42:32 +00:00Commented Jul 19, 2015 at 4:42
-
I need to export the data and do some further processing. When I export is as dbf, NULL is converted to zero .. So I use -999 to be able to distinguish zero from NULL values.Iced_– Iced_2015年07月19日 17:53:07 +00:00Commented Jul 19, 2015 at 17:53
2 Answers 2
ArcPy did not have a Data Access module until ArcGIS 10.1. This will explain your second error message.
The only way to specify fields in ArcPy cursors prior to that was by using their names rather than an index.
-
2And arcpy.*Cursors do not support the
with
statement.user2856– user28562015年07月18日 21:53:16 +00:00Commented Jul 18, 2015 at 21:53 -
Additionally, you need be careful while doing this row update, since you have different field types as looping the fields (i.e., fieldList) and need to elaborate "-999" differently for some.fatih_dur– fatih_dur2015年07月19日 03:25:21 +00:00Commented Jul 19, 2015 at 3:25
I updated to ArcMap 10.2.
This worked for all types of fields:
import arcpy
fc ="C:\gSSURGO_test.gdb\chor_comp_zone1"
fieldObs = arcpy.ListFields (fc)
fieldNames = []
for field in fieldObs:
fieldNames.append(field.name)
fieldCount = len(fieldNames)
curU = arcpy.da.UpdateCursor(fc, fieldNames)
for row in curU:
rowU = row
for field in range(fieldCount):
if rowU[field] == None:
rowU[field] = -999
curU.updateRow(rowU)