Select to view content in your preferred language

Recieving KeyError when trying to run a script that joins a feature class to a related table and copies date values from the table to feature layer

664
1
01-12-2023 11:41 AM
griff
by
New Contributor

Below is the script I have been attempting to run, I have cannibalized it from several python scripts I have found online.

with arcpy.da.UpdateCursor(fcPath, fcFields) as cursor:
       for row in cursor:
              joinID = row[0]
              relatedRecord = dict_RelTbl[joinID]
              currentPosition = relatedRecord[2]
              row[1] = currentPosition
              cursor.updateRow(row)

print ("sucess")

After I run this portion of the script I get this error 

KeyError                                  Traceback (most recent call last)
In  [14]:
Line 4:     relatedRecord = dict_RelTbl[joinID]

KeyError: '{F9F6909A-83FE-4C56-9320-28BC1C37F98A}'

How do I solve this? I have not found anything online that has been helpful.

import arcpy

workspace = r"fgdb"
fcPath = r"feature_layer"
relatedTblPath = r"table"

print ("vehicle inventory and vehicle inspection paths set")

edit = arcpy.da.Editor(r"fgdb")
edit.startEditing(False, True)
edit.startOperation()

print ('in edit session')

relTblFields = ["relglobal", "oildate"]
fcFields = ["GlobalID", "oildate"]

print ("field dict's created")
dict_RelTbl = {}

dateQry="oildate IS NOT NULL"

#Loop through related table and store only the max date for each unique ID.
with arcpy.da.SearchCursor(relatedTblPath, relTblFields) as cursor:
       for row in cursor:
             joinID = row[0]
             if joinID not in dict_RelTbl.keys():
                  dict_RelTbl[joinID] = row
# if the key is in the dictionary already (one to many relationship), compare the dates and keep the most recent date
else:
         curDate = dict_RelTbl[joinID][1]
         if row[1] > curDate:
              dict_RelTbl[joinID] = row

with arcpy.da.UpdateCursor(fcPath, fcFields) as cursor:
         for row in cursor:
               joinID = row[0]
               relatedRecord = dict_RelTbl[joinID]
               currentPosition = relatedRecord[2]
               row[1] = currentPosition
               cursor.updateRow(row)

print ("sucess")

edit.stopOperation()
edit.stopEditing(True)

print ("done editing")

 

Thank you all!

Tags (1)
0 Kudos
1 Reply
JamesGough
Regular Contributor

KeyError is a standard python exception when you try to access a dictionary key that does not exist. To avoid your script crashing when accessing a non-existant key; either use try/except block or use the get() method of dictionary to access the key (this will return None if the key does not exist instead of throwing an exception.

Try/Except example:

 

try:
    relatedRecord = dict_RelTbl[joinID]
except KeyError:
    print(f"{joinID} not in dict_RelTbl")

 

 

get() example:

 

relatedRecord = dict_RelTbl.get(joinID)
if relatedRecord is None:
    print(f"{joinID} not in dict_RelTbl")

 

You could also check if the key exists before trying to access it (joinID in dict_RelTbl), but I prefer the above methods.