Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bixb0012/c282d47ef5b7bafffb107e1d28b11c63 to your computer and use it in GitHub Desktop.
Save bixb0012/c282d47ef5b7bafffb107e1d28b11c63 to your computer and use it in GitHub Desktop.
ArcPy: Multi-value To Single-value Attribute
#!python
# Reference: 1) https://pro.arcgis.com/en/pro-app/latest/arcpy/functions/describe.htm
# 2) https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/create-feature-class.htm
# 3) https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/create-table.htmarc
# 4) https://pro.arcgis.com/en/pro-app/latest/arcpy/get-started/data-access-using-cursors.htm
# 5) https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/clear-workspace-cache.htm
import arcpy
from arcpy.management import CreateFeatureclass , CreateTable, ClearWorkspaceCache
in_table = # path to feature class or table with multi-value concatenated attributes
out_table = # path to feature class or table for single-value attributes
explode_field = # field in in_table containing values to explode
delimiter = # text delimiter separating values
# Example 1: Explode record(s) with multi-value concatenated attributes into multiple records
# with single-value attributes
# get all fields and remove system fields
system_field_attrs = (
'OIDFieldName',
'shapeFieldName',
'lengthFieldName',
'areaFieldName',
'globalIDFieldName'
)
desc = arcpy.Describe(in_table)
fields = {field.name.upper(): field.type for field in desc.fields}
for attr in system_field_attrs:
_ = fields.pop(getattr(desc, attr, "").upper(), None)
# test explode_field exists and is of type 'String'
field_name = explode_field.upper()
if field_name not in fields:
raise TypeError(f"The field '{explode_field}' does not exist")
field_type = fields[field_name]
if field_type != 'String':
raise TypeError(f"Expected 'String', got '{field_type}'")
field_names = list(fields)
field_index = field_names.index(field_name)
# determine whether to call CreateFeatureclass or CreateTable for output
if hasattr(desc, "shapeFieldName"):
SR = desc.spatialReference
res = CreateFeatureclass(path, name, template=in_table, spatial_reference=SR)
field_names = ["SHAPE@"] + field_names
field_index += 1
else:
res = CreateTable(path, name, template=in_table)
# populate multi-values into single-value records
with arcpy.da.InsertCursor(res, field_names) as icur:
with arcpy.da.SearchCursor(in_table, field_names) as scur:
for row in scur:
for i in row[field_index].split(delimiter):
new_row = list(row)
new_row[field_index] = i.strip()
icur.insertRow(new_row)
ClearWorkspaceCache()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment