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.
1 Answer 1
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:
-
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 - gis.stackexchange.com/questions/53816/….Alex Tereshenkov– Alex Tereshenkov2016年09月16日 15:55:36 +00:00Commented Sep 16, 2016 at 15:55
-
I will certainly check those links out. thank you for your time and help!Casey P.– Casey P.2016年09月16日 18:05:35 +00:00Commented Sep 16, 2016 at 18:05