Delete numeric characters from fields

679
3
Jump to solution
05-04-2017 09:17 AM
CCWeedcontrol
Regular Contributor

I have a feature class with some fields that i would like to remove the numeric characters from but just the numbers and only from the fist 3 characters of the attribute i need to able to keep the # and numbers at the end.

The fields look like this

FireDist HighwayDist SchoolDist
FireDist  699 HWY #4763 School #132
710 Rural Fire690 HWY #1762 School #131
720 Rural Fire693 HWY #2770 School #139
NOT in First Dist696 HWY #3765 School #134

I am not sure who to use arcpy to only look at the first 3 characters and if they are numeric to strip them and to strip it from all 3 fields at once. I would be grateful for some help.

Thanks.

0 Kudos
1 Solution

Accepted Solutions
RandyBurton
MVP Regular Contributor

Try something like:

>>> hwy = "699 HWY #4"
>>> hwy
'699 HWY #4'
>>> print hwy.lstrip('0123456789.- ')
HWY #4

View solution in original post

3 Replies
RandyBurton
MVP Regular Contributor

Try something like:

>>> hwy = "699 HWY #4"
>>> hwy
'699 HWY #4'
>>> print hwy.lstrip('0123456789.- ')
HWY #4
JoshuaBixby
MVP Esteemed Contributor

Python String constants can also be used:

>>> import string
>>> hwy = "699 HWY #4"
>>> hwy
'699 HWY #4'
>>> hwy.lstrip(string.digits + string.punctuation + string.whitespace)
'HWY #4'
>>> 
JoshuaBixby
MVP Esteemed Contributor

In addition to using built-in string methods, there is a fairly straightforward regular expression approach:

>>> import re
>>> hwy = "699 HWY #4"
>>> pattern = re.compile(r"[^\W\d_]", re.UNICODE)
>>> hwy[pattern.search(hwy).start():]
'HWY #4'
>>>