Skip to content

Instantly share code, notes, and snippets.

@jasonbot
Created July 12, 2012 19:37
Show Gist options
  • Save jasonbot/3100403 to your computer and use it in GitHub Desktop.
Save jasonbot/3100403 to your computer and use it in GitHub Desktop.
Get namedtuples instead of lists from an arcpy.da SearchCursor
import collections
def rows_as_namedtuples(cursor):
col_tuple = collections.namedtuple('Row', cursor.fields)
for row in cursor:
yield col_tuple(*row)
with arcpy.da.SearchCursor(r'c:\data\world.gdb\world_cities', '*') as sc:
for row in rows_as_namedtuples(sc):
print sc.CITY_NAME
@maphew
Copy link

maphew commented Feb 13, 2019

there's also rename=True

If rename is true, invalid fieldnames are automatically replaced with positional names.
For example, ['abc', 'def', 'ghi', 'abc'] is converted to ['abc', '_1', 'ghi', '_3'], eliminating
the keyword def and the duplicate fieldname abc."
(ref)

at the cost of some name predictability. Both rename methods in concert could be useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment