Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have an ArcPy Data Access update cursor that I would like to sort before making updates to it.

The cursor makes updates if I don't sort, but if I add a sort to the cursor I get an error, "iteration not started."

The sort is working correctly but it must be busting the cursor for some reason.

Is it not possible to sort an update cursor like a search cursor? Does it change the type or something?

    idList = ['100100', '100200', '100300', '200100']
    count = 0
    with arcpy.da.UpdateCursor(newFC, ("SHAPE@X", "SHAPE@Y", "Label")) as addLabelCursor:            
        for row in sorted(addLabelCursor, key=itemgetter(0), reverse = True): 
            print idList[count]
            row[2] = str(idList[count])
            addLabelCursor.updateRow(row)
            count += 1
share|improve this question
    
If I watch the locals in a debug session the "addLabelCursor" remains a da.UpdateCursor object all the way through. Its not like its getting converted to a list when its sorted. –  TurboGus Aug 9 '12 at 16:07
    
It is failing at: addLabelCursor.updateRow(row) –  TurboGus Aug 9 '12 at 16:08

1 Answer 1

up vote 8 down vote accepted

The sort completely exhausts the cursor before you go into updateRow. The way that cursors work is that the updateRow call doesn't work on arbitrary rows out-of-seqence, but on the current one being iterated over. That is, you can only operate on one row at a time.

You may want to look into doing this on the geodatabase side instead. You can specify an ORDER BY clause and sort it before you get to it instead.

share|improve this answer
    
It looks like you cannot use the geometry tokens in the sql clause of a cursor. I'll have to use a work around like adding it to a real field before hand or maybe I will just store this cursors input in memory and use a search cursor to sort and insert cursor to create. Thanks for confirming that I cannot sort an update cursor and still update. –  TurboGus Aug 9 '12 at 17:43
1  
@TurboGus but you can sort the rows in a cursor. The help indicates you can specify the sql_clause (tuple) parameter: sql_clause=(None, 'ORDER BY SORT_FIELD ASC') -- or DESC for descending. –  nmpeterson Oct 17 at 20:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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