Field Calculate Need Last Word of String

6049
16
08-17-2016 10:16 AM
CindyLewis
Deactivated User

I am working with street names and need only the suffix returned (Dr, Blvd, St, Pkwy)

ei. Mountain View Blvd      = Blvd

I attempted to go from the right to left and looking for the first space " " from the right (between View _ Blvd) to split?

in VBA if possible

Thanks

0 Kudos
16 Replies
DanPatterson_Retired
MVP Emeritus

but I covered reordering things yesterday

>>> a = "Mountain View Farms Blvd"
>>> r = "{3}, {0} {1} {2} ".format(*a.split(" "))
>>> r
'Blvd, Mountain View Farms '
CindyLewis
Deactivated User

Thank you and I appreciate your help!

0 Kudos
TedKowal
Honored Contributor

I don't deal with addressing much... what little bit I had to deal with frustrated me to no end... there are no one standard for addressing so the last word in the string may not be the street type, could be a range, box number ..... so if you are looking to pull out the street type, street name ect.... what you need is a more comprehensive app to do so; an address parser..... that said... I did have some luck with an old parsing python script I found and modified slightly to fit my needs... as you can see it is more involved than the simple field calculator scripts and still at least for the Miami area is only 90% effective in parsing....   I do not remember the source of the original script so my apologies to the person who hard effort went in making it.

from pyparsing import *
def ParseAddress(address):
# define number as a set of words
    units = oneOf("Zero One Two Three Four Five Six Seven Eight Nine Ten"
              "Eleven Twelve Thirteen Fourteen Fifteen Sixteen Seventeen Eighteen Nineteen",
              caseless=True)
    tens = oneOf("Ten Twenty Thirty Forty Fourty Fifty Sixty Seventy Eighty Ninety",caseless=True)
    hundred = CaselessLiteral("Hundred")
    thousand = CaselessLiteral("Thousand")
    OPT_DASH = Optional("-")
    numberword = ((( units + OPT_DASH + Optional(thousand) + OPT_DASH + 
                      Optional(units + OPT_DASH + hundred) + OPT_DASH + 
                      Optional(tens)) ^ tens ) 
                   + OPT_DASH + Optional(units) )

    # number can be any of the forms 123, 21B, 222-A or 23 1/2
    housenumber = originalTextFor( numberword | Combine(Word(nums) + 
                        Optional(OPT_DASH + oneOf(list(alphas))+FollowedBy(White()))) + 
                        Optional(OPT_DASH + "1/2")
                        )
    numberSuffix = oneOf("st th nd rd").setName("numberSuffix")
    streetnumber = originalTextFor( Word(nums) + 
                     Optional(OPT_DASH + "1/2") +
                     Optional(numberSuffix) )

    # just a basic word of alpha characters, Maple, Main, etc.
    name = ~numberSuffix + Word(alphas)

    # types of streets - extend as desired
    type_ = Combine( MatchFirst(map(Keyword,"ST BLVD LN RD AVE PASS "
                            "TRL PATH PSGE WAY LANE"
                            "CIR DR PKWY CT SQ "
                            "LP TER TERR PL".split())) + Optional(".").suppress())

    # street name 
    nsew = Combine(oneOf("N S E W North South East West NW NE SW SE") + Optional("."))
    streetName = (Combine( Optional(nsew) + streetnumber + 
                            Optional("1/2") + 
                            Optional(numberSuffix), joinString=" ", adjacent=False )
                    ^ Combine(~numberSuffix + OneOrMore(~type_ + Combine(Word(alphas) + Optional("."))), joinString=" ", adjacent=False) 
                    ^ Combine("Avenue" + Word(alphas), joinString=" ", adjacent=False)).setName("streetName")

    # PO Box handling
    acronym = lambda s : Regex(r"\.?\s*".join(s)+r"\.?")
    poBoxRef = ((acronym("PO") | acronym("APO") | acronym("AFP")) + 
                 Optional(CaselessLiteral("BOX"))) + Word(alphanums)("boxnumber")

    # basic street address
    streetReference = streetName.setResultsName("name") + Optional(type_).setResultsName("type")
    direct = housenumber.setResultsName("number") + streetReference
    intersection = ( streetReference.setResultsName("crossStreet") + 
                     ( '@' | Keyword("and",caseless=True)) +
                     streetReference.setResultsName("street") )
    streetAddress = ( poBoxRef("street")
                      ^ direct.setResultsName("street")
                      ^ streetReference.setResultsName("street")
                      ^ intersection )

    # how to add Apt, Suite, etc.
    suiteRef = (
                oneOf("Suite Ste Apt Apartment Room Rm #", caseless=True) + 
                Optional(".") + 
                Word(alphanums+'-')("suitenumber"))A
    streetAddress = streetAddress + Optional(Suppress(',') + suiteRef("suite"))

    return streetAddress.parseString(address, parseAll=True)

a = ParseAddress("611 NW 23 CT")
print a
print "Number: " + a.street.number
print "Name  : " + a.street.name
print "Type  : " +a.street.type
print
print "-----------"
if a.street.boxnumber:
    print "Box:", addr.street.boxnumber
print a.dump
JoeBorgione
MVP Emeritus

....there are no one standard for addressing...

As I like to say; that's the thing about standards, there are plenty to choose from....

That should just about do it....
DanPatterson_Retired
MVP Emeritus

and as the post says.... Field Calculate Need Last Word of String  

0 Kudos
TedKowal
Honored Contributor

I also saw ... need only the suffix returned (Dr, Blvd, St, Pkwy) 

... Just giving a little experience ....  I deal with transportation... but need something similar... only the last word in string.... turned into a nightmare ---

(I had a bunch of Johnson Dr. ---- got instead a bunch Johnson's no drives -- )

0 Kudos
DanPatterson_Retired
MVP Emeritus

It just reminds me of:

The little question that grew...  vs  The little answer that grew

can't figure out where this one fit