I'm trying to change st. to street, ave. to avenue, etc. using .replace() for addresses in a single cell. For example: WEST ST. should be WEST STREET or MOUNT PEBBLE RD. should be MOUNT PEBBLE ROAD
Here is my code:
if 'STREET' not in address and address.find('ST.'):
address = address.replace('ST','STREET')
The result gives me WESTREET STREET. How can I leave the address name untouched without altering the address name? I tried .split() but most cells had different list lengths so it got really confusing.
Try using Regex with boundaries.
Ex:
import re
s = """WEST ST
MOUNT PEBBLE RD"""
toReplace = {"ST": 'STREET', "RD": "ROAD", "ave": "avenue"}
for k,v in toReplace.items():
s = re.sub(r"\b" + k + r"\b", v, s)
print(s)
Output:
WEST STREET
MOUNT PEBBLE ROAD
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With