<?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: Update Cursor/If Statement in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/update-cursor-if-statement/m-p/1087037#M61971</link>
    <description>&lt;P&gt;maybe a case issue or whitespace?&amp;nbsp; Maybe ditch the cursor for a moment and just add some print statements to debug.&lt;/P&gt;</description>
    <pubDate>Mon, 09 Aug 2021 09:16:38 GMT</pubDate>
    <dc:creator>DavidPike</dc:creator>
    <dc:date>2021-08-09T09:16:38Z</dc:date>
    <item>
      <title>Update Cursor/If Statement</title>
      <link>https://community.esri.com/t5/python-questions/update-cursor-if-statement/m-p/1087014#M61968</link>
      <description>&lt;P&gt;I am trying to use the Update Cursor to look-up a key in a dictionary I created, Region_dic and if it finds the key, it will update the row with the value. However, for some reason, the if statement equals false, when it should be true. Thanks for your help!&lt;/P&gt;&lt;P&gt;keyRegion='directory pathway'&lt;/P&gt;&lt;P&gt;with arcpy.da.UpdateCursor(keyRegion,["Region","ID"]&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; for row in cursor:&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if row[0] in Region_dic:&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; row[1]==Region_dic.get(row[0])&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cursor.updateRow(row)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Aug 2021 04:40:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-cursor-if-statement/m-p/1087014#M61968</guid>
      <dc:creator>ThiPham12</dc:creator>
      <dc:date>2021-08-09T04:40:01Z</dc:date>
    </item>
    <item>
      <title>Re: Update Cursor/If Statement</title>
      <link>https://community.esri.com/t5/python-questions/update-cursor-if-statement/m-p/1087037#M61971</link>
      <description>&lt;P&gt;maybe a case issue or whitespace?&amp;nbsp; Maybe ditch the cursor for a moment and just add some print statements to debug.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Aug 2021 09:16:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-cursor-if-statement/m-p/1087037#M61971</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-08-09T09:16:38Z</dc:date>
    </item>
    <item>
      <title>Re: Update Cursor/If Statement</title>
      <link>https://community.esri.com/t5/python-questions/update-cursor-if-statement/m-p/1087044#M61974</link>
      <description>&lt;P&gt;Hi, Try to use print statement before&amp;nbsp;&lt;SPAN&gt;cursor.updateRow(row). so that if there is an error you'll find it.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Aug 2021 10:23:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-cursor-if-statement/m-p/1087044#M61974</guid>
      <dc:creator>broy06</dc:creator>
      <dc:date>2021-08-09T10:23:02Z</dc:date>
    </item>
    <item>
      <title>Re: Update Cursor/If Statement</title>
      <link>https://community.esri.com/t5/python-questions/update-cursor-if-statement/m-p/1087053#M61980</link>
      <description>&lt;P&gt;In my opinion you will have to loop the dictionary as well to find the right key? Maybe try something like this?&lt;/P&gt;&lt;LI-CODE lang="python"&gt;with arcpy.da.UpdateCursor(keyRegion,["Region","ID"]) as cursor:

    for row in cursor:

        for key, value in Region_dic.iteritems():

            if key == row[0]:

                print key, value
                row[1] = value

                cursor.updateRow(row)

            else:
                print ("does not fit")&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 09 Aug 2021 11:42:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-cursor-if-statement/m-p/1087053#M61980</guid>
      <dc:creator>JohannesBierer</dc:creator>
      <dc:date>2021-08-09T11:42:21Z</dc:date>
    </item>
    <item>
      <title>Re: Update Cursor/If Statement</title>
      <link>https://community.esri.com/t5/python-questions/update-cursor-if-statement/m-p/1087064#M61982</link>
      <description>&lt;P&gt;The 'if row[0] in Region_dic' and Region_dict.get() methods iterate through/ lookup the dictionary already.&lt;/P&gt;&lt;P&gt;I'd wonder if the key types are the same between the dictionary and the row[0] value being checked as well as what &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/167692"&gt;@DavidPike&lt;/a&gt; said with Case differences.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;with arcpy.da.UpdateCursor(keyRegion,["Region","ID"]) as cursor:
    for row in cursor:
        val = Region_dic.get(row[0], 'Not Found')  # you can set this to default to other default values (like the default None) if the key is not found in the dict.
        if val != 'Not Found': 
            row[1] = val
            cursor.updateRow(row)
        else:
            print(f'{row[0]} was not found. Check the dictionary keys for differences.')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Aug 2021 12:42:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-cursor-if-statement/m-p/1087064#M61982</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-08-09T12:42:45Z</dc:date>
    </item>
    <item>
      <title>Re: Update Cursor/If Statement</title>
      <link>https://community.esri.com/t5/python-questions/update-cursor-if-statement/m-p/1087853#M62027</link>
      <description>&lt;P&gt;I think you copied your code wrong, but just in case:&lt;/P&gt;&lt;P&gt;The first line is incomplete (closing paranthesis and "as cursor:")&lt;/P&gt;&lt;LI-CODE lang="python"&gt;row[1]==Region_dic.get(row[0])
# You're using two equal signs, which compares row[1] to Region_dic[row[0]];
# this line will compute to True or False, but it won't set row[1].

# what you want is this:
row[1] = Region_dic.get(row[0])&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You actually don't need the manual check for the key, you can just catch the KeyError:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;keyRegion = 'directory pathway'
with arcpy.da.UpdateCursor(keyRegion, ["Region","ID"]) as cursor:
    for r, i in cursor:
        try:
            i = Region_dic[r]
            cursor.updateRow([r, i])
        except KeyError:
            print("{} was not found.".format(r))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Aug 2021 12:10:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-cursor-if-statement/m-p/1087853#M62027</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2021-08-11T12:10:25Z</dc:date>
    </item>
    <item>
      <title>Re: Update Cursor/If Statement</title>
      <link>https://community.esri.com/t5/python-questions/update-cursor-if-statement/m-p/1088238#M62063</link>
      <description>&lt;P&gt;HI Prabal,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your help. When I add the else: 'not found', the result is not found. When I tried the print statement: print('row[0] not found'), there is an error "error return without exception set."&lt;/P&gt;</description>
      <pubDate>Thu, 12 Aug 2021 02:33:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-cursor-if-statement/m-p/1088238#M62063</guid>
      <dc:creator>ThiPham12</dc:creator>
      <dc:date>2021-08-12T02:33:00Z</dc:date>
    </item>
  </channel>
</rss>

