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)
Solved! Go to Solution.
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.
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
Why not apply a definition query and use the calculate field tool?
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.
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
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