Select to view content in your preferred language

Use Field Calculator to Generate Alphanumeric Values

78
1
Wednesday
ChrisCyphers
Emerging Contributor

I am using ArcMap 10.6 and have a text field called WAMID.  The field values are alphanumeric and sequential. As new features are added this field is null.  I would like help writing a python script where once a week or so I can use field calculator to calculate sequential values for this field.  The most recent value for this field is WMN06022087 so the script should fill in this field with WMN06022088, WMN06022089, WMN06022090, WMN06022091, etc.

 

0 Kudos
1 Reply
JakeSkinner
Esri Esteemed Contributor

Hi @ChrisCyphers,

Below is an example on how you could do this:

import arcpy

# Variable
table = r'c:\TEMP\PYTHON\Test.gdb\addresses'

# Get Max Val
print("Get Max Val")
vals = [row[0].split("WMN0")[-1] for row in arcpy.da.SearchCursor(table, ["WAMID"], "WAMID IS NOT NULL")]
maxVal = int(max(vals))

# Update field
print("Update field")
with arcpy.da.UpdateCursor(table, ["WAMID"], "WAMID IS NULL") as cursor:
    for row in cursor:
        newVal = maxVal + 1
        row[0] = "WMN0" + str(newVal)
        cursor.updateRow(row)
        maxVal = newVal
del cursor

print("Finished")