Select to view content in your preferred language

Populate empty/black rows from an existing field

606
3
01-05-2022 12:09 PM
CCWeedcontrol
Frequent Contributor

I have the following but It's only works to populate for Field1, I would like to populate Field1 blanks or Nulls with Field2 attributes and Field2 blanks or nulls with Field1. I have the the following but it only works to populate Field1..

Maybe I am going about this the wrong way?

flds = ['FID', 'Field1','Field2'] # FID shapefile, OID geodatabase

search_feats = {f[0]:(f[1:]) for f in arcpy.da.SearchCursor(fc1,flds)}

with arcpy.da.UpdateCursor(fc1,flds) as upd_cur:
     for upd_row in upd_cur:
          if upd_row[0] in search_feats.keys():
               if upd_row[1] == None:
                    upd_row[1] = search_feats[upd_row[0]][1]
                    upd_cur.updateRow(upd_row) 
               else: 
                    pass
del upd_cur  

 

 

 

0 Kudos
3 Replies
GKmieliauskas
Esri Regular Contributor

Hi,

Try this code:

with arcpy.da.UpdateCursor(fc1,flds) as upd_cur:
    for upd_row in upd_cur:
        updated = False
        if upd_row[1] == None:
            upd_row[1] = upd_row[0]
            updated = True
        if upd_row[0] == None:
            upd_row[0] = upd_row[1]
            updated = True
        if updated == True:
            upd_cur.updateRow(upd_row)

To speed up process you can add whereclause parameter to your UpdateCursor to check is one of fields is None or empty

CCWeedcontrol
Frequent Contributor

Thank you I appreciate the replay. I do have something similar with a arcpy.da.UpdateCurosr. I was trying to see how it would be done with a dictionary key.

 

I was think of adding another "if" but I am not 100% sure if I am understand the dictionary keys right.

 

 

with arcpy.da.UpdateCursor(fc1,flds) as upd_cur:
     for upd_row in upd_cur:
          if upd_row[0] in search_feats.keys():
               if upd_row[1] == None:
                    upd_row[1] = search_feats[upd_row[0]][1]
               if upd_row[2] == None:
                    upd_row[2] = search_feats[upd_row[0]][0]
                    upd_cur.updateRow(upd_row)

 

 

0 Kudos
CCWeedcontrol
Frequent Contributor

my indentation on line 8 was incorrect.

 

with arcpy.da.UpdateCursor(fc, flds) as upd_cur:
    for upd_row in upd_cur:
        if upd_row[0] in search_feats.keys():
            if upd_row[1] in ("", ' ', None):
                upd_row[1] = search_feats[upd_row[0]][1]
            if upd_row[2] in ("", ' ', None):
                upd_row[2] = search_feats[upd_row[0]][0]
            upd_cur.updateRow(upd_row)
0 Kudos