Created
July 12, 2012 19:37
-
-
Save jasonbot/3100403 to your computer and use it in GitHub Desktop.
Get namedtuples instead of lists from an arcpy.da SearchCursor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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
If you're accessing fields via tokens ("SHAPE@XY", "OID@", etc.), you'll want to make the following change since