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
table = "data.dbf" 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() 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 = int(list2[-1]) - int(list2[0]) 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) del row, cursor except: pass print "Finished Script"
import arcpy from arcpy import env env.overwriteOutput = True env.workspace = r"C:\Users\test\Desktop\TestPythonScripts" table = "data.dbf" 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() list2 = [] for FEAT_SEQ in list: 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"
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"
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 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.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
Runtime error Traceback (most recent call last): File "<string>", line 9, in <module> RuntimeError: An invalid SQL statement was used. [SELECT id, age, OBJECTID FROM WAYNE WHERE id = <built-in function id>]
import arcpy from arcpy import env env.workspace = r"C:\temp\python\test.gdb" table = "table" list2 = [] with arcpy.da.SearchCursor(table, ["id", "age"], "id = " + str(id)) as cursor: for row in cursor: #append age to new list list2.append(row[1]) #sort list list2.sort() try: #calculate the difference difference = list2[-1] - list2[-2] except: pass del row, cursor try: #update table with difference value with arcpy.da.UpdateCursor(table, ["id", "age", "difference"], "age = " + str(list2[-2]) + " AND id = " + str(id)) as cursor: for row in cursor: row[2] = difference cursor.updateRow(row) except: pass del row, cursor
import arcpy from arcpy import env env.workspace = r"C:\temp\python\test.gdb" table = "data" # create empyt list to append ID field list = [] # iterate through table and append ID to list with arcpy.da.SearchCursor(table, ["id"]) 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 id in list: list2 = [] with arcpy.da.SearchCursor(table, ["id", "age", "difference"], "id = " + str(id)) as cursor: for row in cursor: #append age 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, ["id", "age", "difference"], "age = " + str(list2[0]) + " AND id = " + str(id)) as cursor: for row in cursor: row[2] = difference cursor.updateRow(row) except: pass del row, cursor
Just so I understand you correctly. In your example:ID AGE NEWID DIFFERENCE2259 1955 22591955 n/a 2259 1956 22591956 1 2259 1986 22591986 n/a 2259 1989 22591989 n/aThe 'Difference' value is not being calculated between 1986 and 1989 because the age from 1955 and 1956 is larger when compared to todays date. Is that correct?
Hi Clinton,Here is an example on how you can do this:import arcpy from arcpy import env env.workspace = r"C:\temp\python\test.gdb" table = "data" # create empyt list to append ID field list = [] # iterate through table and append ID to list with arcpy.da.SearchCursor(table, ["id"]) 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 id in list: list2 = [] with arcpy.da.SearchCursor(table, ["id", "age", "difference"], "id = " + str(id)) as cursor: for row in cursor: #append age to new list list2.append(row[1]) #sort list list2.sort() try: #calculate the difference difference = list2[-1] - list2[-2] except: pass del row, cursor try: #update table with difference value with arcpy.da.UpdateCursor(table, ["id", "age", "difference"], "age = " + str(list2[-2]) + " AND id = " + str(id)) as cursor: for row in cursor: row[2] = difference cursor.updateRow(row) except: pass del row, cursor
import arcpy from arcpy import env env.workspace = r"C:\temp\python\test.gdb" table = "data" # create empyt list to append ID field list = [] # iterate through table and append ID to list with arcpy.da.SearchCursor(table, ["id"]) 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 id in list: list2 = [] with arcpy.da.SearchCursor(table, ["id", "age", "difference"], "id = " + str(id)) as cursor: for row in cursor: #append age to new list list2.append(row[1]) #sort list list2.sort() try: #calculate the difference difference = list2[-1] - list2[-2] except: pass del row, cursor try: #update table with difference value with arcpy.da.UpdateCursor(table, ["id", "age", "difference"], "age = " + str(list2[-2]) + " AND id = " + str(id)) as cursor: for row in cursor: row[2] = difference cursor.updateRow(row) except: pass del row, cursor
Membros conectados podem postar, seguir atualizações e mais. Novo aqui? Registre uma conta gratuita.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.