<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: 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 laye in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/recieving-keyerror-when-trying-to-run-a-script/m-p/1247819#M66526</link>
    <description>&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;Try/Except example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;try:
    relatedRecord = dict_RelTbl[joinID]
except KeyError:
    print(f"{joinID} not in dict_RelTbl")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;get() example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;relatedRecord = dict_RelTbl.get(joinID)
if relatedRecord is None:
    print(f"{joinID} not in dict_RelTbl")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You could also check if the key exists before trying to access it &lt;EM&gt;(joinID in dict_RelTbl)&lt;/EM&gt;, but I prefer the above methods.&lt;/P&gt;</description>
    <pubDate>Thu, 12 Jan 2023 20:54:24 GMT</pubDate>
    <dc:creator>JamesGough</dc:creator>
    <dc:date>2023-01-12T20:54:24Z</dc:date>
    <item>
      <title>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</title>
      <link>https://community.esri.com/t5/python-questions/recieving-keyerror-when-trying-to-run-a-script/m-p/1247761#M66524</link>
      <description>&lt;P&gt;Below is the script I have been attempting to run, I have cannibalized it from several python scripts I have found online.&lt;/P&gt;&lt;P&gt;with arcpy.da.UpdateCursor(fcPath, fcFields) as cursor:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;for row in cursor:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; joinID = row[0]&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; relatedRecord = dict_RelTbl[joinID]&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; currentPosition = relatedRecord[2]&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; row[1] = currentPosition&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cursor.updateRow(row)&lt;BR /&gt;&lt;BR /&gt;print ("sucess")&lt;/P&gt;&lt;P&gt;After I run this portion of the script I get this error&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;KeyError&lt;/SPAN&gt;                                  Traceback (most recent call last)
In  &lt;SPAN class=""&gt;[14]&lt;/SPAN&gt;:
Line &lt;SPAN class=""&gt;4&lt;/SPAN&gt;:     relatedRecord = dict_RelTbl[joinID]

&lt;SPAN class=""&gt;KeyError&lt;/SPAN&gt;: '{F9F6909A-83FE-4C56-9320-28BC1C37F98A}'&lt;/PRE&gt;&lt;P&gt;How do I solve this? I have not found anything online that has been helpful.&lt;/P&gt;&lt;P&gt;import arcpy&lt;/P&gt;&lt;P&gt;workspace = r"fgdb"&lt;BR /&gt;fcPath = r"feature_layer"&lt;BR /&gt;relatedTblPath = r"table"&lt;/P&gt;&lt;P&gt;print ("vehicle inventory and vehicle inspection paths set")&lt;/P&gt;&lt;P&gt;edit = arcpy.da.Editor(r"fgdb")&lt;BR /&gt;edit.startEditing(False, True)&lt;BR /&gt;edit.startOperation()&lt;/P&gt;&lt;P&gt;print ('in edit session')&lt;/P&gt;&lt;P&gt;relTblFields = ["relglobal", "oildate"]&lt;BR /&gt;fcFields = ["GlobalID", "oildate"]&lt;/P&gt;&lt;P&gt;print ("field dict's created")&lt;BR /&gt;dict_RelTbl = {}&lt;/P&gt;&lt;P&gt;dateQry="oildate IS NOT NULL"&lt;/P&gt;&lt;P&gt;#Loop through related table and store only the max date for each unique ID.&lt;BR /&gt;with arcpy.da.SearchCursor(relatedTblPath, relTblFields) as cursor:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;for row in cursor:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;joinID = row[0]&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if joinID not in dict_RelTbl.keys():&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; dict_RelTbl[joinID] = row&lt;BR /&gt;# if the key is in the dictionary already (one to many relationship), compare the dates and keep the most recent date&lt;BR /&gt;else:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;curDate = dict_RelTbl[joinID][1]&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if row[1] &amp;gt; curDate:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; dict_RelTbl[joinID] = row&lt;/P&gt;&lt;P&gt;with arcpy.da.UpdateCursor(fcPath, fcFields) as cursor:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;for row in cursor:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;joinID = row[0]&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;relatedRecord = dict_RelTbl[joinID]&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;currentPosition = relatedRecord[2]&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;row[1] = currentPosition&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;cursor.updateRow(row)&lt;BR /&gt;&lt;BR /&gt;print ("sucess")&lt;/P&gt;&lt;P&gt;edit.stopOperation()&lt;BR /&gt;edit.stopEditing(True)&lt;/P&gt;&lt;P&gt;print ("done editing")&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you all!&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jan 2023 19:41:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/recieving-keyerror-when-trying-to-run-a-script/m-p/1247761#M66524</guid>
      <dc:creator>griff</dc:creator>
      <dc:date>2023-01-12T19:41:42Z</dc:date>
    </item>
    <item>
      <title>Re: 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 laye</title>
      <link>https://community.esri.com/t5/python-questions/recieving-keyerror-when-trying-to-run-a-script/m-p/1247819#M66526</link>
      <description>&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;Try/Except example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;try:
    relatedRecord = dict_RelTbl[joinID]
except KeyError:
    print(f"{joinID} not in dict_RelTbl")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;get() example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;relatedRecord = dict_RelTbl.get(joinID)
if relatedRecord is None:
    print(f"{joinID} not in dict_RelTbl")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You could also check if the key exists before trying to access it &lt;EM&gt;(joinID in dict_RelTbl)&lt;/EM&gt;, but I prefer the above methods.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jan 2023 20:54:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/recieving-keyerror-when-trying-to-run-a-script/m-p/1247819#M66526</guid>
      <dc:creator>JamesGough</dc:creator>
      <dc:date>2023-01-12T20:54:24Z</dc:date>
    </item>
  </channel>
</rss>

