Trying to use dictionary key with UpdateCurosor

2213
11
Jump to solution
10-09-2020 08:32 AM
2Quiker
Occasional Contributor II

I can do it with a basic arcpy.da.UpdateCurosr but I am trying to figure out how to use arcpy.da.UpdateCurosr with dictionary key. I am trying to populate Field1 if the row is blank but if not then continue on to the next row. I have the following but I get error,

upd_row[0] = search_feats[upd_row[1]] #FIELD_2
KeyError: 'Blah Text'
>>> 

flds = ['Field1','Field']
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] is None:
        if upd_row[0] != None:
        #if upd_row[0] in ("", ' ', None):               
            upd_row[0] = search_feats[upd_row[1]] #FIELD1
            upd_cur.updateRow(upd_row)


del upd_cur‍‍‍‍‍‍‍‍‍‍‍‍‍‍
11 Replies
2Quiker
Occasional Contributor II

OK, I think what is hold me up is that there are some 'Nulls' in field2.

0 Kudos
JoeBorgione
MVP Emeritus

It would work best in place of joining two features on a common field to copy values from one feature to the other.

2 Quiker‌, that is the basis of the infamous Richard Fairhurst post which in my view is a classic.  It's because of his post that I got into using dictionaries.  As Randy Burton suggests, they are helpful in other instances as well.

That should just about do it....