script for entering alphabetical text field values

12-13-2021 09:02 AM
Labels (1)
New Contributor

Hi ESRI Community -

I'm using ArcGIS Pro 2.9.0.

I was looking to automatically generate text field values (Sub-compartment field) that can be assigned to a pre-exisiting numeric field (Compartments).

eg - I have polygons assigned to compartment numbers 1 through to 50, with multiple polygons assigned to each compartment. 

What Id like to do in the sub-compartment field is to automatically generate a unique alphabetical text label that is assigned to each compartment polygon - ie 1a,1b,1c, 2a, 2b...2z,...etc.  Where there are more than 26 polygons for a given compartment, the labels can go to 1a1,1b1 etc, to save manually entering values for each polygon.

Any suggestions please?



0 Kudos
4 Replies
Regular Contributor

Hi @RhysLlewellyn,

If you are looking to accomplish assigning unique values to component within the range of the main component, then a more suggested manner in which to accomplish this would be to use a counter. Simply put, a counter will automatically assign a unique incrementing value to each sub-compartment of the main compartment. 

So one of several ways to assign the unique sub-compartment values is:



import arcpy

updates = {}

feature_class = #file location for the feature class
fields = # list of fields to which to update the feature class based on the compartment field and the sub-compartment field

with arcpy.da.UpdateCursor(feature_class, fields) as Main_Cursor: # update feature class
    for row in Main_Cursor: # loop through the feature
        if row[0] not in updates and row[1] is not None:
            updates[row[0]] = row[1]
        elif row[0] in updates and row[1] is None: # check if row[0] in updates dict
            update_value = updates[row[0]] + 1 # acquire value from updates dict and increase by 1
            updates[row[0]] = update_value # update key with updated value

            row[0] = row[0] # assign row[0] same value as self
            row[1] = updates[row[0]] # assign row[1] as updates value
            Main_Cursor.updateRow(row) # update row

            updates[row[0]] = 0 # assign row[0] as key and 0 as value
            row[0] = row[0] # assign row[0] same value as self
            row[1] = updates[row[0]] # assign row[1] value of updates[row[0]]
            Main_Cursor.updateRow(row) # update row
del Main_Cursor # del cursor



What you would like to do would be a more complicated approach than simply using a counter.

0 Kudos
New Contributor

Thanks for the tip RGPIS, will let you know how I get on.

0 Kudos
MVP Esteemed Contributor

Conceptually with python:

  • Select Compartment Polygon (probably from a list)
  • Spatially select those subcompartments within the compartment
  • Get a count of the number of subcompartments
  • If Count <= 26
    • Call an update cursor routine that updates the the subcompartmentField with CompartmentID and a letter
  • else if count > 25 and < 51
    • call a different cursor

See code below for what I came up with.


import string

alphaList = list(string.ascii_lowercase)
   value = 0
    compartmentID = 25 # from a list of your compartments feature class
                       #you'll need to nest all of this in another loop
    NumberofSubCompartments = 15 + 1 # get 15 from the count of sub- 
    for count in range(1,NumberofSubCompartments):
        if value <= 25:
            a = alphaList[value]
            value +=1
        elif value > 25 and value < 51:
            for a in alphaList:
                value += 1


No warranties or guarantees.  This is conceptual at best.  Plug in different values  in line 6 that would be the count of sub-compartments.  Those lines that use the print(fString), create the value to plug into the update cursor.  

subCompartmentId = f'{compartmentID}{a}'
#add your update cusor here and

subcompartmentId = f'{compartmentID}{a}{value}'
#add your update cursor here...


That should just about do it....
0 Kudos
New Contributor

Thanks Joe - I'll have a look.

0 Kudos