Ok I think I’m close but need a little more help. I have a
table like the one attached. I’m trying to create a line segment from it
that goes from 22.473 to 27.346 but they are on separate lines. I need a
python script to calculate the to_measure from OBJECTID 1 equal to the
to_measure from OBJECTID2. Do you think that is possible?
I thought this might work but no dice..
table = "Export_Output_3" ridList = [] with arcpy.da.SearchCursor(table, ["RID"]) as cursor: for row in cursor: ridList.append(row[0]) del cursor ridList = set(ridList) for RID in ridList: toMeasList = [] with arcpy.da.SearchCursor(table, ["RID", "to_MEAS"], "RID = '" + RID + "'") as cursor: for row in cursor: toMeasList.append(row[1]) del cursor toMeasList.sort() with arcpy.da.UpdateCursor(table, ["to_MEAS"], "RID = '" + RID + "'") as cursor: for row in cursor: row[0] = toMeasList[-1] cursor.updateRow(row) del cursor toMeasList.sort() with arcpy.da.UpdateCursor(table, ["OBJECTID"], "OBJECTID = '2'") as cursor: for row in cursor: row[1] cursor.deleteRow(row) del cursor
Try:
with arcpy.da.SearchCursor(table, ["RID", "to_MEAS"], "RID = '" + RID + "'")
Otherwise, provide error message.
I’m sure that will work but I keep getting a syntax error so must have a indent wrong.
Well, line 26 (row[1]) looks wrong. Gee, an error message would sure be useful...
Sorry, I looked it up and I needed to use UpdateCursor but now I get the following error..
File "
TypeError: deleteRow() takes no arguments (1 given)
deleteRow() deletes the current row, so no need to tell it which row.
cursor.deleteRow()
I tried that but it deletes both rows. I only wanted to delete the last row in the array after the to_measure was set.
I tried this but get a syntaxError at line 21 and don't see what the indent issue is..
table = "Export_Output_3" ridList = [] with arcpy.da.SearchCursor(table, ["RID"]) as cursor: for row in cursor: ridList.append(row[0]) del cursor ridList = set(ridList) for RID in ridList: toMeasList = [] with arcpy.da.SearchCursor(table, ["RID", "to_MEAS"], "RID = '" + RID + "'") as cursor: for row in cursor: toMeasList.append(row[1]) del cursor toMeasList.sort() with arcpy.da.UpdateCursor(table, ["to_MEAS"], "RID = '" + RID + "'") as cursor: for row in cursor: row[0] = toMeasList[-1] cursor.updateRow(row) del cursor toMeasList.sort() with arcpy.da.UpdateCursor(table, ["OBJECTID"]) as cursor: for row in cursor: if row[1] == 2: cursor.deleteRow() del cursor
You can (and possibly, must) remove all lines of 'del cursor'. Using the 'with' statement means the variable 'cursor' will be deleted on completion, so it may not be there to delete whatsoever.