2

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.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jul 18, 2015 at 19:28
4
  • What version of ArcGIS for Desktop are you using? Commented Jul 18, 2015 at 19:57
  • I am using ArcMap 10.0 Commented 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. Commented 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. Commented Jul 19, 2015 at 17:53

2 Answers 2

4

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.

answered Jul 18, 2015 at 20:08
2
  • 2
    And arcpy.*Cursors do not support the with statement. Commented 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. Commented Jul 19, 2015 at 3:25
2

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)
answered Jul 19, 2015 at 4:23

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.