Basic attribute transfer

558
4
Jump to solution
03-07-2022 11:13 AM
2Quiker
Occasional Contributor II

I am trying to transfer attributes within a table from field "MU" to "MU_update" field but I get this error and I think it is because of my were_Clause but I am not 100% sure.

 

"upd_row[1] = search_feats[upd_row[0]]
KeyError: 1"

 

import arcpy

env.workspace = "C:/Temp/PermitTest/PermitsTestData1/Permits.gdb"

sourceFC = "C:/Temp/Temp.gdb/Table1"
where_clause = "MU LIKE '%TZ%'"
search_feats ={f[0]:f[1] for f in arcpy.da.SearchCursor(sourceFC,["OBJECTID",'MU'],where_clause)}
#searchVals = set(search_feats)
#print(search_feats)

with arcpy.da.UpdateCursor(sourceFC,["OBJECTID","MU_update"]) as upd_cur:
    for upd_row in upd_cur:
        upd_row[1] = search_feats[upd_row[0]]
        upd_cur.updateRow(upd_row)

 

0 Kudos
2 Solutions

Accepted Solutions
DavidPike
MVP Frequent Contributor

Your dictionary only contains the OIDs which match your where_clause, but your update cursor is trying all OID keys - the problem being that some OIDs don't exist in your dictionary and throw the error.

You can throw in a try except

for upd_row in upd_cur:
        try:
            upd_row[1] = search_feats[upd_row[0]]
            upd_cur.updateRow(upd_row)
        except:
            continue

but using the same where_clause on the update cursor will probably be much faster.

View solution in original post

0 Kudos
DavidPike
MVP Frequent Contributor

There's probably much better ways of doing this.

sourceFC = "C:/Temp/Temp.gdb/Table1"
where_clause = "MU LIKE '%TZ%'"
search_feats ={f[0]:[f[1], f[2]] for f in arcpy.da.SearchCursor(sourceFC,["OBJECTID",'MU', 'KL'],where_clause)}
#searchVals = set(search_feats)
#print(search_feats)

with arcpy.da.UpdateCursor(sourceFC,["OBJECTID","MU_update", "KL_update"]) as upd_cur:
for upd_row in upd_cur:
    try:
       upd_row[1] = (search_feats[upd_row[0]])[0]
       upd_row[2] = (search_feats[upd_row[0]])[1]
       upd_cur.updateRow(upd_row)
    except:
       continue

View solution in original post

4 Replies
TonyContreras_Frisco_TX
Occasional Contributor III

Why not apply a definition query and use the calculate field tool?

0 Kudos
DavidPike
MVP Frequent Contributor

Your dictionary only contains the OIDs which match your where_clause, but your update cursor is trying all OID keys - the problem being that some OIDs don't exist in your dictionary and throw the error.

You can throw in a try except

for upd_row in upd_cur:
        try:
            upd_row[1] = search_feats[upd_row[0]]
            upd_cur.updateRow(upd_row)
        except:
            continue

but using the same where_clause on the update cursor will probably be much faster.

0 Kudos
2Quiker
Occasional Contributor II

If I wanted to update another field (3rd) from that same table how would I do that?

 

 

 

sourceFC = "C:/Temp/Temp.gdb/Table1"
where_clause = "MU LIKE '%TZ%'"
search_feats ={f[0]:(f[1:]} for f in arcpy.da.SearchCursor(sourceFC,["OBJECTID",'MU', 'KL'],where_clause)}
#searchVals = set(search_feats)
#print(search_feats)

with arcpy.da.UpdateCursor(sourceFC,["OBJECTID","MU_update", "KL_update"]) as upd_cur:
for upd_row in upd_cur:
    try:
       upd_row[1] = search_feats[upd_row[0]]
       upd_row[2] = search_feats[upd_row[0]][2] ????
       upd_cur.updateRow(upd_row)
    except:
       continue

 

 

 

0 Kudos
DavidPike
MVP Frequent Contributor

There's probably much better ways of doing this.

sourceFC = "C:/Temp/Temp.gdb/Table1"
where_clause = "MU LIKE '%TZ%'"
search_feats ={f[0]:[f[1], f[2]] for f in arcpy.da.SearchCursor(sourceFC,["OBJECTID",'MU', 'KL'],where_clause)}
#searchVals = set(search_feats)
#print(search_feats)

with arcpy.da.UpdateCursor(sourceFC,["OBJECTID","MU_update", "KL_update"]) as upd_cur:
for upd_row in upd_cur:
    try:
       upd_row[1] = (search_feats[upd_row[0]])[0]
       upd_row[2] = (search_feats[upd_row[0]])[1]
       upd_cur.updateRow(upd_row)
    except:
       continue