import arcpy fc = "C:\Program Files (x86)\ArcGIS\Desktop10.2\ArcGlobeData\continent.shp" fields = ["CONTINENT"] where = "CONTINENT = 'Asia'" cursor = arcpy.da.SearchCursor(fc, fields, where) for i in range(0,5): row = cursor.next() print(row)
Solved! Go to Solution.
import itertools import arcpy fc = "C:\Program Files (x86)\ArcGIS\Desktop10.2\ArcGlobeData\continent.shp" fields = ["CONTINENT"] where = "CONTINENT = 'Asia'" with arcpy.da.SearchCursor(fc, fields, where) as cursor: for row in itertools.islice(cursor, 5): print row
Using the older arcpy.SearchCursor, the next() method would return Nothing once the last record had been passed.
Using the newer arcpy.da.SearchCursor, I'm finding that the next() method crashes the script if there are no more records.
(I know there is an alternative method using for row in cursor: but my point is that the next() method should not throw an error after the last record. Also, in my case I'm looking for situations where there are no records so I can perform an action - this doesn't work when using for row in cursor:).
fc = "C:\Program Files (x86)\ArcGIS\Desktop10.2\ArcGlobeData\continent.shp" fields = ["CONTINENT"] where = "CONTINENT = 'Asia'" k = 0 with arcpy.da.SearchCursor(fc, fields, where) as cursor: for row in cursor: print(row) k += 1 if k == 0: print "No records!"
with arcpy.da.SearchCursor(outFC, fields, whereClause) as sCursor: try: #See if there are any records returned. If this throws an error, this means #there are no records, in which case we need to add one sRow = sCursor.next() except: #Create a new point in the output geodatabase pt = (lon, lat) iCursor.insertRow([pt, ID])
if int(arcpy.GetCount_management(parcels).getOutput(0)) > 0:
When I want to verify a selection was made I use code like this ("parcels" is a layer that may or may not contain a selection):if int(arcpy.GetCount_management(parcels).getOutput(0)) > 0:
parcelsCount = int(arcpy.GetCount_management(parcels).getOutput(0)) if 0 < parcelsCount < 2: # do something elif 1 < parcelsCount < 1000: # do something else else: # do a different thing
fc = "C:\Program Files (x86)\ArcGIS\Desktop10.2\ArcGlobeData\continent.shp" fields = ["CONTINENT"] where = "CONTINENT = 'Asia'" with arcpy.da.SearchCursor(fc, fields, where) as cursor: j = len(cursor) if j > 5: j = 5 for i in range(0, j):
Since a cursor outputs to either a list or a dictionary I believe you can use the len() operator on the cursor output
Unfortunately I don't think that's correct.
[ATTACH=CONFIG]33500[/ATTACH]
I've updated my original question to say that the fact that cursor.next() crashes after the last record should be reflected in the documentation - it's clear there are lots of workarounds to this "feature".
Thanks for the assistance,
Steve
import itertools import arcpy fc = "C:\Program Files (x86)\ArcGIS\Desktop10.2\ArcGlobeData\continent.shp" fields = ["CONTINENT"] where = "CONTINENT = 'Asia'" with arcpy.da.SearchCursor(fc, fields, where) as cursor: for row in itertools.islice(cursor, 5): print row