AnsweredAssumed Answered

Problems with an Update Cursor

Question asked by jborgion Champion on Aug 24, 2018
Latest reply on Aug 26, 2018 by jborgion

ArcGIS ArcMap 10.5.1

 

I'm having an issue with an update cursor that has me dead in the water.

 

In my script, I run a da search cursor and create a list of a specific attribute from a table:

 

arcpy.MakeTableView_management('MyBacteriaCounts','ba_view')

sum_table = 'BA_SampleDate_Summary'
with arcpy.da.SearchCursor(sum_table,'MySampleDate') as cursor:
    for row in cursor:
        date_list.append(row[0])

#date_list = [u'1/29/2014', u'2/27/2013', u'2/13/2013', u'1/14/2016']

 

Then I step through that list and perform a selection.  The selection of course on the table view of that table created a in line 1 above:

for d in date_list:
    select = "MySampleDate = '{}'".format(d)
    arcpy.SelectLayerByAttribute_management("ba_view","NEW_SELECTION",select)

So far so good. As I step through date_ list, I need to perform calculation/update on a given field of the selected set.  That field needs to be unique and is based on a variable value concatenated with another field value with a unique identifier that is logically incremented concatenated to that:

 

pre = 'BA'
newdate = '20160114'
unique_id = 'XYZ'

final_value = '{} {} {}'.format(pre,newdate,unique_id)

 

So if there are 3 records selected and they all have the same value of new date and the final_value would look like this:

 

BA 20160114 ABC1

BA 20160114 ABC2

BA 20160114 ABC3

 

The problem I'm having is getting a unique value for unique_id.  There may be several records selected that will be assigned the same value of newdate (which is just a reformat of 'd' in date_list); outside of an edit session I've tried to perform a arcpy.CalculateField_management() to the table view; that calculation is then reflected in the actual table.  The problem I have with that is the unique_id isn't so unique since I'm making the calculation to the entire selected set.

 

I shifted gears and tried using an arcpy.da.UpdateCursor() while in an edit session.  However, that approach has presented me with a different problem. Apparently, one cannot run and update cursor on a table view.  With the table view records selected, I apply the update cursor to the table itself, but none of the records of the table itself show as selected so my update is applied to the entire 5600 +- records.

 

What am I missing here? I thought when I make a selection to the table view, that selection is then reflected on the table itself, but I'm either under the wrong impression, or I'm doing something wrong.

 

Bottom line:  I'm looking for a way to calculate a unique value of a field for each record of a selected set.

 

(Dan; this relates back to my post a couple days ago to format a time stamp to milliseconds.  I was hoping to use that as my unique_id but haven't been able to get it to work for this application)

Outcomes