Select to view content in your preferred language

Use Field Calculator to Generate Alphanumeric Values

254
2
Jump to solution
10-02-2024 11:54 AM
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 Solution

Accepted Solutions
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")

View solution in original post

2 Replies
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")
ChrisCyphers
Emerging Contributor

Thanks Jake, this works perfect!  I had to run this script in the ArcMap Python window instead of field calculator which isn't a big deal, but it found the most recent field entry and added on sequentially from there.  Thanks again!

0 Kudos