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 #4 | 763 School #132 |
710 Rural Fire | 690 HWY #1 | 762 School #131 |
720 Rural Fire | 693 HWY #2 | 770 School #139 |
NOT in First Dist | 696 HWY #3 | 765 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.
Solved! Go to Solution.
Try something like:
>>> hwy = "699 HWY #4"
>>> hwy
'699 HWY #4'
>>> print hwy.lstrip('0123456789.- ')
HWY #4
Try something like:
>>> hwy = "699 HWY #4"
>>> hwy
'699 HWY #4'
>>> print hwy.lstrip('0123456789.- ')
HWY #4
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'
>>>
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'
>>>