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<SPAN class="punctuation token">.</SPAN>MakeTableView_management<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'MyBacteriaCounts'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'ba_view'</SPAN><SPAN class="punctuation token">)</SPAN>
sum_table <SPAN class="operator token">=</SPAN> <SPAN class="string token">'BA_SampleDate_Summary'</SPAN>
<SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN>sum_table<SPAN class="punctuation token">,</SPAN><SPAN class="string token">'MySampleDate'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> cursor<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> cursor<SPAN class="punctuation token">:</SPAN>
date_list<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#date_list = [u'1/29/2014', u'2/27/2013', u'2/13/2013', u'1/14/2016']</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
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:
<SPAN class="keyword token">for</SPAN> d <SPAN class="keyword token">in</SPAN> date_list<SPAN class="punctuation token">:</SPAN>
select <SPAN class="operator token">=</SPAN> <SPAN class="string token">"MySampleDate = '{}'"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>d<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>SelectLayerByAttribute_management<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"ba_view"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"NEW_SELECTION"</SPAN><SPAN class="punctuation token">,</SPAN>select<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>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 <SPAN class="operator token">=</SPAN> <SPAN class="string token">'BA'</SPAN>
newdate <SPAN class="operator token">=</SPAN> <SPAN class="string token">'20160114'</SPAN>
unique_id <SPAN class="operator token">=</SPAN> <SPAN class="string token">'XYZ'</SPAN>
final_value <SPAN class="operator token">=</SPAN> <SPAN class="string token">'{} {} {}'</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>pre<SPAN class="punctuation token">,</SPAN>newdate<SPAN class="punctuation token">,</SPAN>unique_id<SPAN class="punctuation token">)</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>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)