I am trying to delete rows from a shapefile using arcpy.UpdateCursor. The first thing I did was get a unique list of values from the key field "PolyGUID", then I iterate that list doing a arcpy.UpdateCursor. When inside the cursor I set a counter x=0 then for each record I increment +1. The deleteRow doesn't seem to happen after the second record is deleted. Is the object returned by the UpdateCursor not able to operate in this manner? Here's the code..
import arcpy
from arcpy import env
outPointFromPoint = r"C:\Trash\Nov2011\ResFinalC6.shp"
sql = " \"PolyGUID\" <> ''"
ocur = arcpy.SearchCursor(outPointFromPoint, sql)
listDelete = []
for oc in ocur:
if oc.PolyGUID not in listDelete:
listDelete.append(oc.PolyGUID)
print len(listDelete)
del oc, ocur, x, sql
for lD in listDelete:
sql2 = " \"PolyGUID\" = " + "'" + str(lD) + "'"
upcur = arcpy.UpdateCursor(outPointFromPoint, sql2, "", "", "FMEAS2 A")
x=0
for upc in upcur:
x=x+1
print upc.FMEAS2
#print x
if x==1:
print x
else:
print "--------------------------" + str(x)
print "Deleting: " + str(upc.PolyGUID) + str(upc.FMEAS2)
upcur.deleteRow(upc)
del sql2, upc, upcur, x, lD, listDelete
The PolyGUIDs come from a polygon fc. Having the polygon guid in the fc, ResFinalC6.shp, means that during a LocateFeatureAlongARoute operation.. the record from the polygon had one or more than one resultant record. I need to get rid of the any other records if the result was more than one record. A polygon record intersected the route in more than one place (three times for the problem records). The outcome results in a field displaying the m-value where the intersect occurred. So what I did was for each unique value in PolyGUID get a UpdateCursor..sort the field that holds the M-value (because I need the record with the lowest m-value) and use the x counter to delete those PolyGUIDs that resulted in more than one record (all except the one with the lowest m-value..which is why I did the sort on the UpdateCursor) resulting from the query with the UpdateCursor.
The issue is that after the cursor has gone through record 1 and set x=1..then to record 2 and set x=2 and then delete record 2..the script gets to those PolyGUIDs that show up three times..my print statement gets hit and x gets set to x=3 but the record doesn't get deleted. I have ran this numerous times and it's always the same PolyGUIDs left and they all show up three times before the delete tries. In the python shell window where the printing happens the record 3 gets cursed but no delete.
listDelete
) because your sql statement queries aGUID
, which should by nature make it impossible that you return more than one row forupcur
.