2

I have an attribute table looks like this

enter image description here

How do I convert Nulls into 0?

I've tried to set this table non nullable type but got this:

enter image description here

while the official doc says this:

enter image description here

At least one of them is wrong.

Then I tried many ways to do the job, like this:

RValues=arcpy.SearchCursor(sr)

for RValue in RValues:
    #print (RValue.getValue(field))
    if RValue.getValue(field) == None:
       #print (RValue.getValue(field))
       return 0
    else 
       return 
       float(RValue.getValue(field))

print(RValue.getValue(field))

This gives error 999999, don't know why, possible a bug.

Then I tried this:

RValues=arcpy.SearchCursor(sr)
print (RValues)

for RValue in RValues:
    if RValue.getValue(field) == None:
        print (RValue.getValue(field))
        arcpy.CalculateField_management(sr, "RValue",  esp, "PYTHON3")
    print (RValue.getValue(field))

Well, the first print gives a

<geoprocessing cursor object object at 0x0000025F5C161E70>

the second print does isolate all none items, as there is no null in python.

Here is the trick:

the third print either gives error or turn everything into null or 0,focus on everything, expressions I use are as follows:

esp="!RValue!.replace(None, 0)"

esp2="!RValue! * 0"

esp3="set field = 0 where field is null"

esp4="NVL(RValue,0)"

esp5="select nvl(RValue, 0) from sr.RValue"

esp6="None"

How is that even possible when the if before it already isolate only those nones?

4

In your second version, you've mixed cursors and CalculateField_management(). They don't work together in this way. CalculateField_management() will change the value for ALL RECORDS*, and pays no attention to what is happening in your cursor.

You should use either an UpdateCursor (NOT a SearchCursor) OR use a CalculateField_management(). Not both.

I usually do this sort of thing with just a CalculateField_management(). You can do this in one of two ways:

OPTION 1:

This is quicker than OPTION 2 below (but I posted 2 first, so will leave it there as is). This method only updates the records that need changing and also requires fewer lines. I think it is somewhat easier to read too.

Firstly, either select just the records that you want to change to '0' OR make a feature layer that only includes those records. I prefer the latter, as I feel safer (in the case the select doesn't work, you'll change ALL records; in the case the new feature layer doesn't work, you'll just get an error).

Then change all selected/layer records.

It's merely two lines:

arcpy.MakeFeatureLayer_management(sr, "srLayer", "RValue IS NULL")
arcpy.CalculateField_management("srLayer", "RValue", "0", "PYTHON")

Although you may like to clean up afterwards by deleting the layer with a third line:

arcpy.Delete_management("srLayer")

OPTION 2:

Include a code block which will determine the correct value for all records. Note that this will update records that don't need to be changed, but it will update them with the same value they already have, so not a big deal.

But this means that it is slower than OPTION 1 and also not as good for auditing purposes (eg, if you use editor tracking!).

codeblock = """
    def calcRValue(rvalue):
        if rvalue is None:
            return 0
        else:
            return rvalue
"""
arcpy.CalculateField_management(sr, "RValue", "calcRValue(!RValue!)", "PYTHON", codeblock)

OTHER NOTES:

Don't use arcpy.SearchCursor or arcpy.UpdateCursor, which have been superceded. Use arcpy.da.SearchCursor or arcpy.da.UpdateCursor. They are massively faster (even if marginally more difficult syntax).

I'm not sure what your first version is trying to do, but the last line (print) uses the variable RValue out of scope and I would not expect it to work there. Perhaps it should be indented to be within the loop to print the value each time around the loop? But I'm not sure what it's supposed to be doing there.

*actually, all selected records if there are any, or all records otherwise.

  • You're welcome. :-) – Son of a Beach Jan 4 at 4:02
  • that print there is to check the final result – Karl Tian Jan 4 at 5:13
3

I prefer to use an UpdateCursor to manipulate feature class attributes. However, make sure to use the modern Data Access da cursors. For example:

import arcpy

fc = r'C:\path\to\your\geodatabase.gdb\featureclass'

with arcpy.da.UpdateCursor(fc, "some_field") as cursor:
    for row in cursor:
        if row[0] == None: # None appears as <Null> in attribute table float fields
            row[0] = 0
        cursor.updateRow(row)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.