Select to view content in your preferred language

Remove unwanted numbers and characters from attributes

76
1
Friday
2Quiker
Occasional Contributor II

I have some fields that I need to remove the # and numbers, but I need to keep one that matches exactly HIGHWAY DISTRICT #4. I can't seem to get to bypass "HIGHWAY DISTRICT #4". When I run my code, it removes the #4 from HIGHWAY DISTRICT #4

 

 

import arcpy
import time

fc3 = r"C:\Default.gdb\Table_1"
fld = ['FireDist', 'HighwayDist', 'SchoolDist']

def dump_stuff(val):
    """Remove unwanted characters from fields except 'HIGHWAY DISTRICT #4'"""
    if isinstance(val, str):
        if val.strip() == "HIGHWAY DISTRICT #4":
            return val  # Keep "HIGHWAY DISTRICT #4" unchanged
        else:
            return "".join([i for i in val if not (i.isdigit() or i == '#')])
    return val

start_time = time.time()  # Record the start time

with arcpy.da.UpdateCursor(fc3, fld) as cursor:
    for row in cursor:
        # Process each field in the row
        new_row = [dump_stuff(val) for val in row]
        cursor.updateRow(new_row)

end_time = time.time()  # Record the end time
elapsed_time = end_time - start_time

minutes, seconds = divmod(elapsed_time, 60)
print(f"Process time: {int(minutes)} minutes {int(seconds)} seconds")

 

0 Kudos
1 Reply
DanPatterson
MVP Esteemed Contributor
a = ["HIGHWAY DISTRICT #4 "," HIGHWAY DISTRICT #4", " HIGHWAY DISTRICT #4 "]

for i in a:
    print(i.strip())
    
HIGHWAY DISTRICT #4
HIGHWAY DISTRICT #4
HIGHWAY DISTRICT #4

for i in a:
    if "HIGHWAY DISTRICT #4" in i:
        print(i)
        
HIGHWAY DISTRICT #4 
 HIGHWAY DISTRICT #4
 HIGHWAY DISTRICT #4

it should work.... check your input with a print statement or something to see if does match


... sort of retired...