I need to strip numbers from certain fields. I have the following but it strips all number from the entire table.
Some of these fields have blanks and nulls.
import arcpy
arcpy.env.overwriteOutput = True
table = "C:/temp/EMS_Table"
field = ['Agency', 'fa', 'lz']
for field in [f.name for f in arcpy.Describe(table).fields if f.type == "String"]:
exp = "!{0}!.strip('0123456789.- ')"
arcpy.management.CalculateField(table, field, exp.format(field), "PYTHON")
I grab suggestions from StackOverflow quite a bit, I try to include a link to the original SO thread to include in my GeoNet comment.
see edit above....
from this old thread
keep numbers
fld = "aaasdfasdfasdf12341asdfadfadsf"
"".join([i for i in fld if i.isdigit() or i == " "]).strip()
'12341'
keep text
"".join([i for i in fld if i.isalpha() or i == " "]).strip()
'aaasdfasdfasdfasdfadfadsf'
line 1 becomes your field calculation and you feed 'fld' into it
ie
for fld in fields:
do stuff with the above in your code
After running everything looked fine but there seems to be some "#" symbols in those fields as well, For example Uk05#26. I need to remove those as well.
field = ['Agency', 'fa', 'lz']
for f in field:
exp = "!{0}!.strip('0123456789.- ')"
arcpy.management.CalculateField(table, f, exp.format(f), "PYTHON")
That's thing about naming conventions: they don't mean anything if people won't/don't follow them. Once you get your data cleaned up, then automation works really, really well. You can make yourself crazy trying to automate anomalies only to find another one. And then another.
Sometimes you just have to roll up your sleeves and expect to get your hands dirty...
I am receiving this data and in order for me to make it work for me i have to clean this table up but i get what you are saying.
I like summary tables to find the outliers. A pound sign or dash or some other weird character will jump out in a hurry, and typically the count will be fairly low...
just add # to your strip( '0123456789.- #') list
I did try that before i posted but for some reason it's only removing the # from two fields not all three. I just wasn't sure if i was missing something. Does it matter if there is spaces? for example "UK 05# 56CCO. So in this example it needs to be UKCCO.That's the only thing i am seeing that is different.
if you look at your replace list, make sure there is a space in it