import arcgisscripting gp = arcgisscripting.create() inputFC = r'path to a feature class' inRows = gp.searchcursor(inputFC) inRow = inRows.next() while inRow: ...do something inRow = inRows.next()This is quiet a verbose way to loop through the rows. Fortunately there is a way to turn the cursor into an iterator that can be used in a for loop. This can be done by adding the following short class to your code.
class CursorIterator(object): def __init__(self, cursor): self.cursor = cursor self.cursor.reset() def next(self): n = self.cursor.next() if n is None: self.cursor.reset() raise StopIteration return n def __iter__(self): return selfAs it is nicely explained in Expert Python Programming by Tarek Ziadé we just created an iterator class. This is a container class with two methods namely next and __iter__. Usage of the CursorIterator class looks like this.
rows = gp.searchcursor(inputFC) rowsIterator = CursorIterator(rows) for row in rowsIterator: ... do somethingor shorter
for row in CursorIterator(gp.searchcursor(inputFC)): ... do somethingI hope you enjoyed my first post. Have fun.
Update : As Jason Schreider points out in his comment the CursorIterator class can be easily replaced by the following :
rows = gp.searchcursor(inputFC) for row in iter(rows.next, None): ... do something