TransWikia.com

StopIteration: iteration not started Error when zipping a cursor using arcpy.da.UpdateCursor

Geographic Information Systems Asked by user125266 on February 27, 2021

I’ve run into a StopIteration: iteration not started error while trying to order a table using an update cursor.

Is this caused by zipping the cursor together? I’ve tried updating a different table that’s identical to the one I ordered using a search cursor. The error persisted. There’s not a lot of good information about this error so I’ve decided to write this question primarily as a reference for others who run into this problem.

Here’s the code I’m running on a FileGeoDatabase for now

with arcpy.da.SearchCursor(
    in_table = copy_fc,
    field_names = fields, where_clause = "",
    sql_clause = (None, "order by DATE")) as ordered_cursor:

    sorted = [row for row in ordered_cursor]

sorted_df = pd.DataFrame.from_records(
    data = sorted, columns = fields)

print sorted_df.size

fc = r"U:RAqt3.14159;)"

with arcpy.da.UpdateCursor(in_table = fc, field_names = fields)
    as cursor:

    for (row, item) in zip(
        cursor, building_sorted_df.itertuples(index = False)):
        row = item
        #Error happens on line below
        cursor.updateRow(row)

print "Finished"

Please note, this is not a duplicate because I’m storing my values in a pandas data frame instead of trying to do pairwise iteration, my cursors are written correctly, and I’m not writing to a table while I’m reading from it.

2 Answers

Im assuming you dont want to use the Sort tool. Since you are trying to overwrite each row with the UpdateCursor it would be better to store all values in a list, truncate the fc which will delete all rows, and then use the insertcursor to insert the stored values again:

import arcpy

fc = r'C:Default.gdbAnglePoints_1'
datefield = 'Date'

fields = [f.name for f in arcpy.ListFields(fc) if not f.name.upper().startswith(('OBJ','SH'))]
fields.append('SHAPE@')
sql = """ORDER BY {0}""".format(arcpy.AddFieldDelimiters(fc, datefield))
all_records_sorted = [i for i in arcpy.da.SearchCursor(fc,fields,sql_clause=(None,sql))]

arcpy.TruncateTable_management(in_table=fc)
icur = arcpy.da.InsertCursor(fc,fields)
for record in all_records_sorted:
    icur.insertRow((record))
del icur

enter image description here

Answered by BERA on February 27, 2021

To explain the actual error for anyone coming across this question:

In Python 2.7, unlike 3.x, zip does not return an iterator. As soon as you call it on the cursor, it immediately builds a tuple and exhausts the cursor before even entering the for loop. It's basically equivalent to trying to loop over the same cursor twice, it's just that zip is doing it the first time.

Using izip will allow this to work in 2.7 (from itertools import izip).

Answered by mikewatt on February 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP