Home > database >  Replace a word in an address string with dictionary value using for-loop
Replace a word in an address string with dictionary value using for-loop

Time:03-18

I have an address 2300 S SUPER TEMPLE PL which I expect to get 2300 S SUPER TEMPLE PLACE as a result after spelling out the PL to PLACE. I have a dictionary of abbreviated street names:

st_abbr = {'DR': 'DRIVE',
            'RD': 'ROAD', 
            'BLVD':'BOULEVARD',
            'ST':'STREET', 
            'STE':'SUITE',
            'APTS':'APARTMENTS', 
            'APT':'APARTMENT',
            'CT':'COURT',
            'LN' : 'LANE',
            'AVE':'AVENUE',
            'CIR':'CIRCLE',
            'PKWY': 'PARKWAY',
            'HWY': 'HIGHWAY',
            'SQ':'SQUARE',
            'BR':'BRIDGE',
            'LK':'LAKE',
            'MT':'MOUNT',
            'MTN':'MOUNTAIN',
            'PL':'PLACE',
            'RTE':'ROUTE',
            'TR':'TRAIL'}

with a for-loop, I would like to replace the key in address be spelled out. What I thought I should do is loop through each word in the address, thus I have the address.split(), and if the split match one of the keys in the dictionary, to replace that with a spelled out word.

for key in st_abbr.keys():
        if key in address.split():
            address = address.replace(key, st_abbr[key])
        print(address)

It works perfectly on abbreviated street names but this is what I get 2300 S SUPER TEMPLACEE PLACE. It also replaced the PL within 'TEMPLE' with PLACE, thus it gave me 'TEMPLACEE'. I am trying to modify the for loop to only replace the abbreviated street if the street.split() is the exact match of the dict.keys(). I would like guidance on how to achieve that.

CodePudding user response:

Use a comprehension:

addr = '2300 S SUPER TEMPLE PL'
new_addr = ' '.join(st_abbr.get(c, c) for c in addr.split())
print(new_addr)

# Output
2300 S SUPER TEMPLE PLACE

Can you shed a light the concept behind the .get(c,c) in the context of my problem?

# Equivalent code
' '.join(st_abbr[c] if c in st_abbr else c for c in addr.split())

CodePudding user response:

Not sure whether it's the best idea or not, but regex usually can be helpful in these cases:

import re

def getValue(value):
  before = value.group(1)
  name = value.group("name")
  after = value.group(3)
  if name in st_abbr:
    return before   st_abbr[name]   after
  else:
    return before   name   after

myString = "2300 S SUPER TEMPLE PL"
re.sub("(^|\s) (?P<name>[A-Z]{2,4})($|\s)", getValue,myString)

Output

2300 S SUPER TEMPLE PLACE
  • Related