Solved! Go to Solution.
import arcpy from arcpy import env env.workspace = r"C:\Users\ccooper\Desktop\DATA.gdb\WAYNE" table = "WAYNE" # create empyt list to append ID field list = [] # iterate through table and append ID to list with arcpy.da.SearchCursor(table, ["FEAT_SEQ"]) as cursor: for row in cursor: list.append(row[0]) del row, cursor #remove duplicate IDs list = dict.fromkeys(list) list = list.keys() #iterate through list for FEAT_SEQ in list: list2 = [] with arcpy.da.SearchCursor(table, ["FEAT_SEQ", "YEAR_OF_BIRTH", "DIFFERENCE"], "FEAT_SEQ = " + str(FEAT_SEQ)) as cursor: for row in cursor: #append YEAR_OF_BIRTH to new list list2.append(row[1]) #sort list list2.sort() try: #calculate the difference DIFFERENCE = list2[1] - list2[0] except: DIFFERENCE = '' del row, cursor try: #update table with difference value with arcpy.da.UpdateCursor(table, ["FEAT_SEQ", "YEAR_OF_BIRTH", "DIFFERENCE"], "FEAT_SEQ = " + str(FEAT_SEQ) + AND ("YEAR_OF_BIRTH = " + str(list2[0]) + " OR YEAR_OF_BIRTH = " + str(list2[1]))) as cursor: for row in cursor: row[2] = DIFFERENCE cursor.updateRow(row) except: pass del row, cursor
Runtime error Traceback (most recent call last): File "<string>", line 45, in <module> NameError: name 'row' is not defined
try: #update table with difference value with arcpy.da.UpdateCursor(table, ["FEAT_SEQ", "YEAR_OF_BIRTH", "DIFFERENCE"], "FEAT_SEQ = " + str(FEAT_SEQ) + AND ("YEAR_OF_BIRTH = " + str(list2[0]) + " OR YEAR_OF_BIRTH = " + str(list2[1]))) as cursor: for row in cursor: row[2] = DIFFERENCE cursor.updateRow(row) del row, cursor except: pass
Try deleting the row and cursor within the 'try' statement and you should no longer get this error:try: #update table with difference value with arcpy.da.UpdateCursor(table, ["FEAT_SEQ", "YEAR_OF_BIRTH", "DIFFERENCE"], "FEAT_SEQ = " + str(FEAT_SEQ) + AND ("YEAR_OF_BIRTH = " + str(list2[0]) + " OR YEAR_OF_BIRTH = " + str(list2[1]))) as cursor: for row in cursor: row[2] = DIFFERENCE cursor.updateRow(row) del row, cursor except: pass
try: #update table with difference value with arcpy.da.UpdateCursor(table, ["FEAT_SEQ", "YEAR_OF_BIRTH", "DIFFERENCE"], "FEAT_SEQ = " + str(FEAT_SEQ) + "AND YEAR_OF_BIRTH = " + str(list2[0]) + " OR FEAT_SEQ = " + str(FEAT_SEQ) + "AND YEAR_OF_BIRTH = " + str(list2[1])) as cursor: for row in cursor: row[2] = DIFFERENCE cursor.updateRow(row) del row, cursor except: pass
import arcpy from arcpy import env env.overwriteOutput = True env.workspace = r"C:\Users\Test\Desktop\Delete.gdb" table = "data" # create empyt list to append ID field list = [] # iterate through table and append ID to list with arcpy.da.SearchCursor(table, ["FEAT_SEQ"]) as cursor: for row in cursor: list.append(row[0]) del row, cursor #remove duplicate IDs list = dict.fromkeys(list) list = list.keys() #iterate through list for FEAT_SEQ in list: list2 = [] with arcpy.da.SearchCursor(table, ["FEAT_SEQ", "YEAR_OF_BI", "DIFFERENCE"], "FEAT_SEQ = " + str(FEAT_SEQ)) as cursor: for row in cursor: #append YEAR_OF_BI to new list list2.append(row[1]) #sort list list2.sort() try: #calculate the difference DIFFERENCE = list2[1] - list2[0] print list2[1] print list2[0] print DIFFERENCE except: DIFFERENCE = '' del row, cursor try: #update table with difference value with arcpy.da.UpdateCursor(table, ["FEAT_SEQ", "YEAR_OF_BI", "DIFFERENCE"], "FEAT_SEQ = " + str(FEAT_SEQ) + "AND YEAR_OF_BI = " + str(list2[0]) + " OR FEAT_SEQ = " + str(FEAT_SEQ) + "AND YEAR_OF_BI = " + str(list2[1])) as cursor: for row in cursor: row[2] = DIFFERENCE cursor.updateRow(row) print row del row, cursor except: pass print "Finished Script"
#iterate through list for FEAT_SEQ in list: list2 = [] with arcpy.da.SearchCursor(table, ["FEAT_SEQ", "YEAR_OF_BI", "DIFFERENCE"], "FEAT_SEQ = " + str(FEAT_SEQ)) as cursor: for row in cursor: #append YEAR_OF_BI to new list list2.append(row[1]) #sort list list2.sort() try: #calculate the difference DIFFERENCE = list2[1] - list2[0] print list2[1] print list2[0] print DIFFERENCE except: DIFFERENCE = '' del row, cursor try: #update table with difference value with arcpy.da.UpdateCursor(table, ["FEAT_SEQ", "YEAR_OF_BI", "DIFFERENCE"], "FEAT_SEQ = " + str(FEAT_SEQ) + "AND YEAR_OF_BI = " + str(list2[0]) + " OR FEAT_SEQ = " + str(FEAT_SEQ) + "AND YEAR_OF_BI = " + str(list2[1])) as cursor: for row in cursor: row[2] = DIFFERENCE cursor.updateRow(row) print row del row, cursor except: pass print "Finished Script"