I have a feature class and in that feature class i have a field "SiteAddress" and most attributes t looks good but there are some attributes that have numbers after the address and they are not suite, unit or apartment numbers. I am trying to figure out how to query to select them.
Example of one is 24832 Trunnel Ct 8693, is there a way to select features with numbers after alphabet characters and delete the 8693 from ones that have the numbers after the alphabet characters.
any help would be great.
Thanks.
Solved! Go to Solution.
write yourself a little function using this idea
>>> a = "24832 Trunnel Ct 8693"
>>> b = a.split(" ")
>>> c = b[-1]
>>> c.isnumeric()
True
so basically if the last entry in the parsed list is numeric written in scrunched form
if a.split(" ")[-1].isnumeric(): d = " ".join(a.split(" ")[:-1])
...
>>>
>>> d
'24832 Trunnel Ct'
of course you should have an else section which just returns the original string
Possibly a prettier way, but this should do it (luckily there are only 10 possibilities):
"SiteAddress" LIKE '%1' OR "SiteAddress" LIKE '%2' OR "SiteAddress" LIKE '%3' OR "SiteAddress" LIKE '%4' OR "SiteAddress" LIKE '%5' OR "SiteAddress" LIKE '%6' OR "SiteAddress" LIKE '%7' OR "SiteAddress" LIKE '%8' OR "SiteAddress" LIKE '%9' OR "SiteAddress" LIKE '%0'
write yourself a little function using this idea
>>> a = "24832 Trunnel Ct 8693"
>>> b = a.split(" ")
>>> c = b[-1]
>>> c.isnumeric()
True
so basically if the last entry in the parsed list is numeric written in scrunched form
if a.split(" ")[-1].isnumeric(): d = " ".join(a.split(" ")[:-1])
...
>>>
>>> d
'24832 Trunnel Ct'
of course you should have an else section which just returns the original string
If you are going to be searching within and manipulating strings on a regular basis, I strongly encourage you to check out Pythons Regular expression operations. Regular expressions are a very powerful pattern matching tool/language and one that goes well beyond Python.
Using your example, I believe the following regular expression will work for you:
>>> import re >>> a = "24832 Trunnel Ct 8693" >>> re.sub(r"(.*)\s\d+$", r"\1", a.strip()) '24832 Trunnel Ct'
Dan and Joshua i am not sure how to apply your suggestions.
I tried the following in field calculator but no go.
import re
def strip_num(a):
a = "24832 Trunnel Ct 8693"
re.sub(r"(.*)\s\d+$", r"\1", a.strip())
'24832 Trunnel Ct'
strip_num(!SiteAddress!)
def x(a):
if a.split(" ")[-1].isnumeric():
a = " ".join(a.split(" ")[:-1])
return a
then
x(!SiteAddress!)
now assume that the first value (which I will call 'a') is
a = "24832 Trunnel Ct 8693"
>>> x(a)
'24832 Trunnel Ct'
I forgot to mention that is field is a text field, so it's throw a 000539 error.
Thanks.
That is a unicode thing
Now make a script... don't do this manually... make it into a script run it and does it work? If it does then there is other stuff in your field other than the simple example that you reported
def x(a): if a.split(" ")[-1].isnumeric(): a = " ".join(a.split(" ")[:-1]) return a a = "24832 Trunnel Ct 8693" print(x(a))
Now sure how.
really? you surely don't retype everything when coding do you?