CalculateField in a SearchCursor accidentally returns last value in dataset instead of current value

588
1
Jump to solution
05-13-2020 11:21 AM
MarkJohnson3
New Contributor II

Hello. I'm a python newb and am stumped by the following:

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. 

For example, for the first Object ID below, "1", I would like "M1" to populated in the same row in the Object field.

With the event_object = (str(folderpath).split('/')[1]) 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. 

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.

import arcpy

fc = "E:/filegdb.gdb/featureclass"
infield = "Event"
outfield = "Object"
cursor = arcpy.SearchCursor(fc)

row = cursor.next()
while row:
   event = (row.getValue(infield))
   event_object = (str(event).split('/')[1])
   print event_object
   row = cursor.next()
   arcpy.CalculateField_management(fc,outfield,"'" + event_object + "'",expression_type="PYTHON")


OID,Shape,Event,BeginTime,Object
1,Point Z,MUL201136141671/M1/Points/FCB2S2,2011-12-26 07:35:27,FI9
2,Point Z,MUL201136141671/M2/Points/FCB2S2,2011-12-26 07:35:28,FI9
3,Point Z,MUL201136141671/M3/Points/FCB2S2,2011-12-26 07:35:29,FI9
4,Point Z,MUL201136141671/M4/Points/FCB2S2,2011-12-26 07:35:30,FI9
5,Point Z,MUL201136141671/M5/Points/FCB2S2,2011-12-26 07:35:31,FI9
6,Point Z,MUL201136141671/M6/Points/FCB2S2,2011-12-26 07:35:32,FI9
7,Point Z,MUL201136141671/M7/Points/FCB2S2,2011-12-26 07:35:33,FI9
8,Point Z,MUL201136141671/M8/Points/FCB2S2,2011-12-26 07:35:34,FI9
9,Point Z,MUL201136141671/M9/Points/FCB2S2,2011-12-26 07:35:35,FI9
199,Point Z,MUL201136141671/DT1/Points/FCB2S3,2011-12-26 07:38:58,FI9
200,Point Z,MUL201136141671/DT2/Points/FCB2S3,2011-12-26 07:38:59,FI9
201,Point Z,MUL201136141671/DT3/Points/FCB2S3,2011-12-26 07:39:00,FI9
202,Point Z,MUL201136141671/DT4/Points/FCB2S3,2011-12-26 07:39:01,FI9
203,Point Z,MUL201136141671/DT5/Points/FCB2S3,2011-12-26 07:39:02,FI9
204,Point Z,MUL201136141671/DT6/Points/FCB2S3,2011-12-26 07:39:03,FI9
205,Point Z,MUL201136141671/DT7/Points/FCB2S3,2011-12-26 07:39:04,FI9
206,Point Z,MUL201136141671/DT8/Points/FCB2S3,2011-12-26 07:39:05,FI9
207,Point Z,MUL201136141671/DT9/Points/FCB2S3,2011-12-26 07:39:06,FI9
222,Point Z,MUL201136141671/FI1/Points/FCB2S2,2011-12-26 07:32:00,FI9
223,Point Z,MUL201136141671/FI2/Points/FCB2S2,2011-12-26 07:32:01,FI9
224,Point Z,MUL201136141671/FI3/Points/FCB2S2,2011-12-26 07:32:02,FI9
225,Point Z,MUL201136141671/FI4/Points/FCB2S2,2011-12-26 07:32:03,FI9
226,Point Z,MUL201136141671/FI5/Points/FCB2S2,2011-12-26 07:32:04,FI9
227,Point Z,MUL201136141671/FI6/Points/FCB2S2,2011-12-26 07:32:05,FI9
228,Point Z,MUL201136141671/FI7/Points/FCB2S2,2011-12-26 07:32:06,FI9
229,Point Z,MUL201136141671/FI8/Points/FCB2S2,2011-12-26 07:32:07,FI9
230,Point Z,MUL201136141671/FI9/Points/FCB2S2,2011-12-26 07:32:08,FI9

0 Kudos
1 Solution

Accepted Solutions
DavidPike
MVP Frequent Contributor

You're mixing apples and oranges with the calculate field, just use an arcpy.da.UpdateCursor.

UpdateCursor—Data Access module | Documentation 

Please refer to Dan Patterson‌ 's guide to formatting code when posting here. /blogs/dan_patterson/2016/08/14/script-formatting 

import arcpy

fc = r"E:/filegdb.gdb/featureclass"
field_list = ["Event", "Object]

with arcpy.da.UpdateCursor(fc, field_list) as cursor:
    for row in cursor:
        row[1] = (str(row[0]).split('/')[1])
    cursor.updateRow(row)

View solution in original post

1 Reply
DavidPike
MVP Frequent Contributor

You're mixing apples and oranges with the calculate field, just use an arcpy.da.UpdateCursor.

UpdateCursor—Data Access module | Documentation 

Please refer to Dan Patterson‌ 's guide to formatting code when posting here. /blogs/dan_patterson/2016/08/14/script-formatting 

import arcpy

fc = r"E:/filegdb.gdb/featureclass"
field_list = ["Event", "Object]

with arcpy.da.UpdateCursor(fc, field_list) as cursor:
    for row in cursor:
        row[1] = (str(row[0]).split('/')[1])
    cursor.updateRow(row)