Select to view content in your preferred language

Remove unwanted numbers and characters from attributes

281
2
06-14-2024 11:03 AM
2Quiker
Frequent Contributor

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
2 Replies
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...
HaydenWelch
Frequent Contributor

There could be extra internal whitespace in your values e.g. "HIGHWAY••DISTRICT•#4" which wouldn't be removed by .strip(). To get around this chain the comparisons:

 

 

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

 

You can also just remove all whitespace and focus only on the characters:

 

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