2

I am new to python, and have not successfully coded an Update Cursor. I am unable to understand the documentation.

I have a geodatabase table of wells that i have built using python. I now want to loop through each row and update a field. the table contains a field called 'MD' which is type double and contains a depth value. I need to look at a second field called 'FM_Name' for two text strings that indicate the TOP and BOTTOM of a certain perforation zone. So, the cursor would loop though the rows which are sorted in ascending order, and update 'FM_Name' field with the word "Perf" in between the TOP and BOTTOM for each well.

This table has over 50K records, representing roughly 300 wells, so i really dont want to do this by hand.

Here is the table

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Casey P.
  • 53
  • 3

1 Answers1

5

This code should do the work you need to get done. Make sure the table is sorted as you expect. There is a GP tool Sort that can do that for you.

import arcpy
fc = r'C:\ArcGIS\Default.gdb\wells'

with arcpy.da.UpdateCursor(fc,['MD','FM_Name']) as upd_cur:
    for row in upd_cur: #start iterating rows in the table
        if row[1] == 'TOP': #if the FM_NAME column is 'TOP', then:
            row = upd_cur.next() #take next row in the table
            while row[1] != 'BOTTOM': #if the FM_NAME column is not 'BOTTOM', then:
                row[1] = 'PERF' #set the FM_NAME column to be 'PERF'
                upd_cur.updateRow(row) #update the row (writing changes)
                row = upd_cur.next() #take next row and jump back to the 'while' line

I have made some comments in the code so you will be able to catch up.

The results in the attribute table in ArcMap:

enter image description here

Alex Tereshenkov
  • 29,912
  • 4
  • 54
  • 119
  • 2
    @CaseyP., no problem at all! This scenario is somewhat more difficult to code comparing to a plain "all rows" update. Take your time to learn arcpy. Btw, there is a great list of resources for this - http://gis.stackexchange.com/questions/53816/what-are-some-resources-for-learning-arcpy. – Alex Tereshenkov Sep 16 '16 at 15:55
  • I will certainly check those links out. thank you for your time and help! – Casey P. Sep 16 '16 at 18:05