<?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 CalculateField in a SearchCursor accidentally returns last value in dataset instead of current value in ArcGIS API for Python Questions</title>
    <link>https://community.esri.com/t5/arcgis-api-for-python-questions/calculatefield-in-a-searchcursor-accidentally/m-p/811978#M2499</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello. I'm a python newb and am stumped by the following:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have a feature class in a file geodatabase. I want to go through each feature in my feature class, find the event code in the Event field, extract the chunk of text after the first "/", and then populate the same row in the Object field with that chunk of text only.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For example, for the first Object ID below, "1", I would like "M1" to populated in the same row in the Object field.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;With the&amp;nbsp;&lt;STRONG&gt;event_object = (str(folderpath).split('/')[1])&lt;/STRONG&gt; command, I can successfully find the chunk of text I'm interested in. All that is left is to assign it to the Object field.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Unfortunately, when I attempt to do that, I am somehow stuck with the chunk of text from the last row in the SearchCursor, and it is assigned to the Object field for every feature in the feature class, illustrated in the table data below. There must be something funky with the nature of SearchCursor that I don't understand. How am I grabbing the chunk of text from the last feature in the feature class and getting stuck with it while I'm iterating on features prior to it? How do I defeat this? It's personal now!! Many thanks in advance.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;import arcpy&lt;/P&gt;&lt;P&gt;fc = "E:/filegdb.gdb/featureclass"&lt;BR /&gt;infield = "Event"&lt;BR /&gt;outfield = "Object"&lt;BR /&gt;cursor = arcpy.SearchCursor(fc)&lt;/P&gt;&lt;P&gt;row = cursor.next()&lt;BR /&gt;while row:&lt;BR /&gt;&amp;nbsp; &amp;nbsp;event = (row.getValue(infield))&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;event_object = (str(event).split('/')[1])&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;print event_object&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;row = cursor.next()&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;arcpy.CalculateField_management(fc,outfield,"'" + event_object + "'",expression_type="PYTHON")&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;OID,Shape,Event,BeginTime,Object&lt;BR /&gt;1,Point Z,MUL201136141671/M1/Points/FCB2S2,2011-12-26 07:35:27,FI9&lt;BR /&gt;2,Point Z,MUL201136141671/M2/Points/FCB2S2,2011-12-26 07:35:28,FI9&lt;BR /&gt;3,Point Z,MUL201136141671/M3/Points/FCB2S2,2011-12-26 07:35:29,FI9&lt;BR /&gt;4,Point Z,MUL201136141671/M4/Points/FCB2S2,2011-12-26 07:35:30,FI9&lt;BR /&gt;5,Point Z,MUL201136141671/M5/Points/FCB2S2,2011-12-26 07:35:31,FI9&lt;BR /&gt;6,Point Z,MUL201136141671/M6/Points/FCB2S2,2011-12-26 07:35:32,FI9&lt;BR /&gt;7,Point Z,MUL201136141671/M7/Points/FCB2S2,2011-12-26 07:35:33,FI9&lt;BR /&gt;8,Point Z,MUL201136141671/M8/Points/FCB2S2,2011-12-26 07:35:34,FI9&lt;BR /&gt;9,Point Z,MUL201136141671/M9/Points/FCB2S2,2011-12-26 07:35:35,FI9&lt;BR /&gt;199,Point Z,MUL201136141671/DT1/Points/FCB2S3,2011-12-26 07:38:58,FI9&lt;BR /&gt;200,Point Z,MUL201136141671/DT2/Points/FCB2S3,2011-12-26 07:38:59,FI9&lt;BR /&gt;201,Point Z,MUL201136141671/DT3/Points/FCB2S3,2011-12-26 07:39:00,FI9&lt;BR /&gt;202,Point Z,MUL201136141671/DT4/Points/FCB2S3,2011-12-26 07:39:01,FI9&lt;BR /&gt;203,Point Z,MUL201136141671/DT5/Points/FCB2S3,2011-12-26 07:39:02,FI9&lt;BR /&gt;204,Point Z,MUL201136141671/DT6/Points/FCB2S3,2011-12-26 07:39:03,FI9&lt;BR /&gt;205,Point Z,MUL201136141671/DT7/Points/FCB2S3,2011-12-26 07:39:04,FI9&lt;BR /&gt;206,Point Z,MUL201136141671/DT8/Points/FCB2S3,2011-12-26 07:39:05,FI9&lt;BR /&gt;207,Point Z,MUL201136141671/DT9/Points/FCB2S3,2011-12-26 07:39:06,FI9&lt;BR /&gt;222,Point Z,MUL201136141671/FI1/Points/FCB2S2,2011-12-26 07:32:00,FI9&lt;BR /&gt;223,Point Z,MUL201136141671/FI2/Points/FCB2S2,2011-12-26 07:32:01,FI9&lt;BR /&gt;224,Point Z,MUL201136141671/FI3/Points/FCB2S2,2011-12-26 07:32:02,FI9&lt;BR /&gt;225,Point Z,MUL201136141671/FI4/Points/FCB2S2,2011-12-26 07:32:03,FI9&lt;BR /&gt;226,Point Z,MUL201136141671/FI5/Points/FCB2S2,2011-12-26 07:32:04,FI9&lt;BR /&gt;227,Point Z,MUL201136141671/FI6/Points/FCB2S2,2011-12-26 07:32:05,FI9&lt;BR /&gt;228,Point Z,MUL201136141671/FI7/Points/FCB2S2,2011-12-26 07:32:06,FI9&lt;BR /&gt;229,Point Z,MUL201136141671/FI8/Points/FCB2S2,2011-12-26 07:32:07,FI9&lt;BR /&gt;230,Point Z,MUL201136141671/FI9/Points/FCB2S2,2011-12-26 07:32:08,FI9&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 13 May 2020 18:21:07 GMT</pubDate>
    <dc:creator>MarkJohnson3</dc:creator>
    <dc:date>2020-05-13T18:21:07Z</dc:date>
    <item>
      <title>CalculateField in a SearchCursor accidentally returns last value in dataset instead of current value</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/calculatefield-in-a-searchcursor-accidentally/m-p/811978#M2499</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello. I'm a python newb and am stumped by the following:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have a feature class in a file geodatabase. I want to go through each feature in my feature class, find the event code in the Event field, extract the chunk of text after the first "/", and then populate the same row in the Object field with that chunk of text only.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For example, for the first Object ID below, "1", I would like "M1" to populated in the same row in the Object field.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;With the&amp;nbsp;&lt;STRONG&gt;event_object = (str(folderpath).split('/')[1])&lt;/STRONG&gt; command, I can successfully find the chunk of text I'm interested in. All that is left is to assign it to the Object field.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Unfortunately, when I attempt to do that, I am somehow stuck with the chunk of text from the last row in the SearchCursor, and it is assigned to the Object field for every feature in the feature class, illustrated in the table data below. There must be something funky with the nature of SearchCursor that I don't understand. How am I grabbing the chunk of text from the last feature in the feature class and getting stuck with it while I'm iterating on features prior to it? How do I defeat this? It's personal now!! Many thanks in advance.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;import arcpy&lt;/P&gt;&lt;P&gt;fc = "E:/filegdb.gdb/featureclass"&lt;BR /&gt;infield = "Event"&lt;BR /&gt;outfield = "Object"&lt;BR /&gt;cursor = arcpy.SearchCursor(fc)&lt;/P&gt;&lt;P&gt;row = cursor.next()&lt;BR /&gt;while row:&lt;BR /&gt;&amp;nbsp; &amp;nbsp;event = (row.getValue(infield))&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;event_object = (str(event).split('/')[1])&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;print event_object&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;row = cursor.next()&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;arcpy.CalculateField_management(fc,outfield,"'" + event_object + "'",expression_type="PYTHON")&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;OID,Shape,Event,BeginTime,Object&lt;BR /&gt;1,Point Z,MUL201136141671/M1/Points/FCB2S2,2011-12-26 07:35:27,FI9&lt;BR /&gt;2,Point Z,MUL201136141671/M2/Points/FCB2S2,2011-12-26 07:35:28,FI9&lt;BR /&gt;3,Point Z,MUL201136141671/M3/Points/FCB2S2,2011-12-26 07:35:29,FI9&lt;BR /&gt;4,Point Z,MUL201136141671/M4/Points/FCB2S2,2011-12-26 07:35:30,FI9&lt;BR /&gt;5,Point Z,MUL201136141671/M5/Points/FCB2S2,2011-12-26 07:35:31,FI9&lt;BR /&gt;6,Point Z,MUL201136141671/M6/Points/FCB2S2,2011-12-26 07:35:32,FI9&lt;BR /&gt;7,Point Z,MUL201136141671/M7/Points/FCB2S2,2011-12-26 07:35:33,FI9&lt;BR /&gt;8,Point Z,MUL201136141671/M8/Points/FCB2S2,2011-12-26 07:35:34,FI9&lt;BR /&gt;9,Point Z,MUL201136141671/M9/Points/FCB2S2,2011-12-26 07:35:35,FI9&lt;BR /&gt;199,Point Z,MUL201136141671/DT1/Points/FCB2S3,2011-12-26 07:38:58,FI9&lt;BR /&gt;200,Point Z,MUL201136141671/DT2/Points/FCB2S3,2011-12-26 07:38:59,FI9&lt;BR /&gt;201,Point Z,MUL201136141671/DT3/Points/FCB2S3,2011-12-26 07:39:00,FI9&lt;BR /&gt;202,Point Z,MUL201136141671/DT4/Points/FCB2S3,2011-12-26 07:39:01,FI9&lt;BR /&gt;203,Point Z,MUL201136141671/DT5/Points/FCB2S3,2011-12-26 07:39:02,FI9&lt;BR /&gt;204,Point Z,MUL201136141671/DT6/Points/FCB2S3,2011-12-26 07:39:03,FI9&lt;BR /&gt;205,Point Z,MUL201136141671/DT7/Points/FCB2S3,2011-12-26 07:39:04,FI9&lt;BR /&gt;206,Point Z,MUL201136141671/DT8/Points/FCB2S3,2011-12-26 07:39:05,FI9&lt;BR /&gt;207,Point Z,MUL201136141671/DT9/Points/FCB2S3,2011-12-26 07:39:06,FI9&lt;BR /&gt;222,Point Z,MUL201136141671/FI1/Points/FCB2S2,2011-12-26 07:32:00,FI9&lt;BR /&gt;223,Point Z,MUL201136141671/FI2/Points/FCB2S2,2011-12-26 07:32:01,FI9&lt;BR /&gt;224,Point Z,MUL201136141671/FI3/Points/FCB2S2,2011-12-26 07:32:02,FI9&lt;BR /&gt;225,Point Z,MUL201136141671/FI4/Points/FCB2S2,2011-12-26 07:32:03,FI9&lt;BR /&gt;226,Point Z,MUL201136141671/FI5/Points/FCB2S2,2011-12-26 07:32:04,FI9&lt;BR /&gt;227,Point Z,MUL201136141671/FI6/Points/FCB2S2,2011-12-26 07:32:05,FI9&lt;BR /&gt;228,Point Z,MUL201136141671/FI7/Points/FCB2S2,2011-12-26 07:32:06,FI9&lt;BR /&gt;229,Point Z,MUL201136141671/FI8/Points/FCB2S2,2011-12-26 07:32:07,FI9&lt;BR /&gt;230,Point Z,MUL201136141671/FI9/Points/FCB2S2,2011-12-26 07:32:08,FI9&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 13 May 2020 18:21:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/calculatefield-in-a-searchcursor-accidentally/m-p/811978#M2499</guid>
      <dc:creator>MarkJohnson3</dc:creator>
      <dc:date>2020-05-13T18:21:07Z</dc:date>
    </item>
    <item>
      <title>Re: CalculateField in a SearchCursor accidentally returns last value in dataset instead of current value</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/calculatefield-in-a-searchcursor-accidentally/m-p/811979#M2500</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You're mixing apples and oranges with the calculate field, just use an arcpy.da.UpdateCursor.&lt;/P&gt;&lt;P&gt;&lt;A class="link-titled" href="https://pro.arcgis.com/en/pro-app/arcpy/data-access/updatecursor-class.htm" title="https://pro.arcgis.com/en/pro-app/arcpy/data-access/updatecursor-class.htm" rel="nofollow noopener noreferrer" target="_blank"&gt;UpdateCursor—Data Access module | Documentation&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Please refer to &lt;A href="https://community.esri.com/migrated-users/3116" target="_blank"&gt;Dan Patterson&lt;/A&gt;‌ 's guide to formatting code when posting here.&amp;nbsp;&lt;A href="https://community.esri.com/blogs/dan_patterson/2016/08/14/script-formatting" target="_blank"&gt;/blogs/dan_patterson/2016/08/14/script-formatting&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; arcpy

fc &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; r&lt;SPAN class="string token"&gt;"E:/filegdb.gdb/featureclass"&lt;/SPAN&gt;
field_list &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"Event"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; "Object&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;

&lt;SPAN class="keyword token"&gt;with&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;da&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;UpdateCursor&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;fc&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; field_list&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;as&lt;/SPAN&gt; cursor&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
    &lt;SPAN class="keyword token"&gt;for&lt;/SPAN&gt; row &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; cursor&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
        row&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;1&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt; &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;str&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;row&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;0&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;split&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'/'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;1&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
    cursor&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;updateRow&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;row&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 09:32:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/calculatefield-in-a-searchcursor-accidentally/m-p/811979#M2500</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-12-12T09:32:45Z</dc:date>
    </item>
  </channel>
</rss>

