Select attributes that ends in numbers

4446
9
Jump to solution
06-24-2016 02:25 PM
CCWeedcontrol
Regular Contributor

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.

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Esteemed Contributor

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

View solution in original post

9 Replies
DarrenWiens2
MVP Honored Contributor

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'

DanPatterson_Retired
MVP Esteemed Contributor

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

JoshuaBixby
MVP Esteemed Contributor

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'
0 Kudos
CCWeedcontrol
Regular Contributor

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!) 

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor
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'

CCWeedcontrol
Regular Contributor

I forgot to mention that is field is a text field, so it's throw a 000539 error.

Thanks.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

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))       
CCWeedcontrol
Regular Contributor

Now sure how.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

really? you surely don't retype everything when coding do you?