Skip to content

Instantly share code, notes, and snippets.

@tonjadwyer
Created March 2, 2017 18:52
Show Gist options
  • Save tonjadwyer/0e4162b1423c404dc2a50188c3b3c2f5 to your computer and use it in GitHub Desktop.
Save tonjadwyer/0e4162b1423c404dc2a50188c3b3c2f5 to your computer and use it in GitHub Desktop.
Read an ArcGIS table into a dictionary.
#Source: http://gis.stackexchange.com/questions/54804/fastest-methods-for-modifying-attribute-tables-with-python
# fc - is the full path name to table or feature class
def make_attribute_dict(fc, key_field, attr_list=['*']):
attdict = {}
fc_field_objects = arcpy.ListFields(fc)
fc_fields = [field.name for field in fc_field_objects if field.type != 'Geometry']
if attr_list == ['*']:
valid_fields = fc_fields
else:
valid_fields = [field for field in attr_list if field in fc_fields]
#Ensure that key_field is always the first field in the field list
cursor_fields = [key_field] + list(set(valid_fields) - set([key_field]))
with arcpy.da.SearchCursor(fc, cursor_fields) as cursor:
for row in cursor:
attdict[row[0]] = dict(zip(cursor.fields,row))
return attdict
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment