Using Lists with arcpy.da.UpdateCursor

6609
16
07-13-2018 07:56 AM
deleted-user-_7x2iiEJ9SpB
Deactivated User

I am trying to use arcpy.da.UpdateCursor to find and replace old values in a field with new values.  I have made it work but I will ended up with an ridiculously lard if\elif statement (notice below that I stopped after the first 2 items in the list).  I would like to avoid this by iterating through both list to update the field.  I have tried without any luck.  Any suggestions?  Code is attached.

0 Kudos
16 Replies
deleted-user-_7x2iiEJ9SpB
Deactivated User

The issue i'm running into is that I don't want it to print 100 times "not in list."  I just want it to total up the number of times other values are found.

0 Kudos
JoeBorgione
MVP Emeritus

How about setting a counter variable?

counter = 0
for row in cursor:
        if if row[0] not in LCStatus.keys():
            # print error message and keep original value
            #print "{} not in list".format(row[0])
            counter +=1
        else: # update value
            row[0] = LCStatus[row[0]]
            cursor.updateRow(row)
print '{} rows not in list...'.format(counter)‍‍‍‍‍‍‍‍‍‍
That should just about do it....
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

It seems there may be values in the records that are not in your list.  That is good information to offer up earlier in the thread since it will change the structure of the code. 

Using my original code suggestion, I believe the following will work for you:

import arcpy


fc = r'G:\pworks\assetmanagement\Local Gov Info Model\Wastewater\Inputs\Valve_Schema.gdb\SControlValve'

LifeCycleStatus1 = ["ABN", "ACT","AB","CHGORD","DEM","INA","OTH","PRP","RMV","UNK","FV", "FD"]
LifeCycleStatus2 = ["Abandoned","Active","As Built","Change Order","Demolished","Inactive","Other","Proposed","Removed","Unknown","Field Verification Required","Future Development"]


    
try:    
    cursor = arcpy.da.UpdateCursor(fc, ["LIFECYCLESTATUS"])
    cnt = 0
    for row in cursor:
        if row[0] in LifeCycleStatus1:
            row[0] = LifeCycleStatus2[LifeCycleStatus1.index(row[0])]
            cursor.updateRow(row)
        else:
            cnt += 1
    del row 
    del cursor
    print("{} records didn't have Life Cycle Status values in list".format(cnt))
except RuntimeError:
    pass‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
JoeBorgione
MVP Emeritus

Joshua Bixby‌ - where does 'i' come from in line 18?

That should just about do it....
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

My bad, I have corrected it from "i" to "1".

deleted-user-_7x2iiEJ9SpB
Deactivated User

Joshua Bixby‌ it wasn't my original goal to print out the values until I realized how many we had that were missing\wrong.

0 Kudos
RandyBurton
MVP Alum

Something like this should give a listing with a count of the values in the LIFECYCLESTATUS field:

import arcpy

fc = r'G:\pworks\assetmanagement\Local Gov Info Model\Wastewater\Inputs\Valve_Schema.gdb\SControlValve'
field = 'LIFECYCLESTATUS'

d = {} # new dictionary

with arcpy.da.SearchCursor(fc, (field)) as rows:
    for row in rows:
        if row[0] not in d:
            d[row[0]] = 1
        else:
            d[row[0]] += 1

for k, v in d.iteritems():
    print k, v
0 Kudos