Update only blank, null field attributes

529
4
11-16-2021 10:54 AM
2Quiker
Occasional Contributor II

I need to only update the fields attributes that are blank or Null, there are some attributes in Feilda and I want to skip those but I am not sure how do to do that. I have the following.

 

with arcpy.da.UpdateCursor(fc,['Fielda','COUNT','Field1']) as cursor:
    for row in cursor:
         if row[0] not in (None, "", " "):
            row[2] = ",".join(dict1[row[0]])
            cursor.updateRow(row)

 

0 Kudos
4 Replies
DanPatterson
MVP Esteemed Contributor

dict1

where is this defined?

... sort of retired...
0 Kudos
DominicRoberge2
Occasional Contributor III

still don't know where your dict1 is defined but this code will skip the rows with records

with arcpy.da.UpdateCursor(fc,['Fielda','COUNT','Field1']) as cursor:
    for row in cursor:
         if row[0] not in (None, "", " "):
            row[2] = ",".join(dict1[row[0]])
            cursor.updateRow(row)             
         else:
             print("All good, nothing to do here")
0 Kudos
by Anonymous User
Not applicable

Your conditional as written is testing if the Fielda is not null so its doing the opposite of what you are wanting to do. 

just change your conditional:

if row[0] in [None, "", " "]:

or you can filter nulls in the where clause:

 

with arcpy.da.UpdateCursor(fc, ['Fielda', 'Field1'], 'Fielda IS NULL') as cursor:
   for row in cursor:
      row[1] = ",".join(dict1[row[0]])
      ...

 

 

0 Kudos
2Quiker
Occasional Contributor II

I meant that I need to update Field1 and that Field1 has some attributes and also FieldA sometimes has attributes.

 

 

dict1 = dict()
with arcpy.da.SearchCursor(fc,['FieldA','COUNT','FieldB']) as cursor:
    for row in cursor:
        if row[0] not in (None, "", " "):
            dict1.setdefault(row[0],[]).append(str(row[2]))

with arcpy.da.UpdateCursor(fc,['FieldA','COUNT','Field1']) as cursor:
    for row in cursor:
         if row[2] is None:
            if row[0] not in (None, "", " "):
                row[2] = ",".join(dict1[row[0]])
                cursor.updateRow(row)

 

0 Kudos