0

I have the following script which selects the row with the maximum value in a specified field, and then deletes that record. This works when specifying a single input fc. I am working with ArcGIS 10.5.

fc = # the input feature class  
field = # the field in the fc to sort by  

rows = arcpy.SearchCursor(fc, '', '', '', field + ' D') # sort "field" descending  

for row in rows: # read the rows  
    max = row.getValue(field) # get value in field  
    expr = '"' + field + '" = ' + str(max) # make "Where" expression  
    arcpy.SelectLayerByAttribute_management(fc, "NEW_SELECTION", expr) # make selection  
    arcpy.DeleteRows_management(fc)
    break # exit loop after first time

What I am trying to do, is loop this workflow over a list of feature classes (using ListFeatureClasses). I tried the following solution, which ran but didn't appear to do anything. (I'm running in the Python window at the moment, but would like it to run as a standalone script eventually).

>>> def deleterows(fc):
...     rows = arcpy.SearchCursor(fc, '', '', '', field + ' D') # sort "field" descending  
...   
... for row in rows: # read the rows  
...     max = row.getValue(field) # get value in field  
...     expr = '"' + field + '" = ' + str(max) # make "Where" expression  
...     arcpy.SelectLayerByAttribute_management(fc, "NEW_SELECTION", expr) # make selection  
...     arcpy.DeleteRows_management(fc)
...     break # exit loop after first time
...     
>>> for fc in fc_list:
...     deleterows(fc)

fc_list = arcpy.ListFeatureClasses() # this is already defined before the above code runs. 

I'm sure the answer is quite simple, but I'm still getting to grips with Python loops.

  • I would recommend only using new style (arcpy.da) cursors. – PolyGeo Sep 19 '17 at 11:55
  • 1
    1) While old-style cursors aren't deprecated, you certainly shouldn't be learning to use them: use Data Access (da) cursors instead. 2) Learn how to write scripts in a .py file: they are much easier to reproduce and debug 3) fix your indent scheme -- the rows for loop is not within the function. – Vince Sep 19 '17 at 12:11
1

I think your issue is with indention. The below lines need a further indentation:

... for row in rows: # read the rows  
...     max = row.getValue(field) # get value in field  
...     expr = '"' + field + '" = ' + str(max) # make "Where" expression  
...     arcpy.SelectLayerByAttribute_management(fc, "NEW_SELECTION", expr) # make selection  
...     arcpy.DeleteRows_management(fc)
...     break # exit loop after first time

To improve your code, a data access UpdateCursor would be a more efficient solution.

def deleterows (fc, field):
    sqlClause = "ORDER BY {} DESC".format (field)
    with arcpy.da.UpdateCursor (fc, "*", sql_clause = (None, sqlClause)) as curs:
        curs.next ()
        curs.deleteRow ()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.