Select to view content in your preferred language

Attribute not set to NULL in python script using UpdateCursor

202
5
Jump to solution
Monday
LoriEmersonKDOT
Regular Contributor

When I set a field to None in an arcpy script, it shows as None in the attribute table rather than <Null> when using an UpdateCursor.  Why does it set it to a text value of "None" rather than <Null>?

 

# ---------------------------------------------------------------
# Loop through NEW to OLD spatial join results
#     If attributes don't match, then show the differences in the
#     new Changes_ attribute fields.
# ---------------------------------------------------------------
with arcpy.da.UpdateCursor(
    fc, fields, where_clause = "Join_Count=1"
) as cursor:
    for row in cursor:
        myADDR = ""
                                   
        #Compare L_F_ADD = L_F_ADD_1 and L_T_ADD = L_T_ADD_1 and R_F_ADD = R_F_ADD_1 and R_T_ADD = R_T_ADD_1
        if row[4] != row[24]: 
            myADDR += "L_F_ADD (new = old): " + str(row[4]) + "=" + str(row[24]) + " | "
        if row[5] != row[25]:
            myADDR += "L_T_ADD (new = old): " + str(row[5]) + "=" + str(row[25]) + " | "
        if row[6] != row[26]:
            myADDR += "R_F_ADD (new = old): " + str(row[6]) + "=" + str(row[26]) + " | "
        if row[7] != row[27]:
            myADDR += "R_T_ADD (new = old): " + str(row[7]) + "=" + str(row[27]) + " | "
             
              
        if myADDR == "":
            myADDR = None
                    
        row[43] = myADDR
               
        cursor.updateRow(row)
Tags (3)
0 Kudos
1 Solution

Accepted Solutions
LoriEmersonKDOT
Regular Contributor

I fixed the problem by removing the Default of None when adding the field to the existing feature class attribute table.  Now the python assignment of None works when performing the updateRow within the UpdateCursor.UpdateCursorDefaultValue.png

View solution in original post

0 Kudos
5 Replies
DanPatterson
MVP Esteemed Contributor

is it a featureclass in a geodatabase that you are tryig to update? or a shapefile? (which doesn't support null


... sort of retired...
JoshuaBixby
MVP Esteemed Contributor

In this case, i.e., trying to NULL a shape file field that doesn't know/support NULL, the DA update cursor will generate a RuntimeError instead of populating the field with "None":

Traceback (most recent call last):
  File "<string>", line 4, in <module>
RuntimeError: The field is not nullable. [TXT_FLD]
0 Kudos
LoriEmersonKDOT
Regular Contributor

Hi Dan,

It's a feature class in a file geodatabase.  I even set the Default value to NULL (None) when adding the new field, so I know it accepts nulls.

UpdateCursorNullField.png

0 Kudos
LoriEmersonKDOT
Regular Contributor

I fixed the problem by removing the Default of None when adding the field to the existing feature class attribute table.  Now the python assignment of None works when performing the updateRow within the UpdateCursor.UpdateCursorDefaultValue.png

0 Kudos
AlfredBaldenweck
MVP Regular Contributor

That's super odd. Do you have the same problem with a different table?

0 Kudos