...

/

Case Study: Street Addresses

Case Study: Street Addresses

We'll cover the following...

This series of examples was inspired by a real-life problem I had in my day job several years ago, when I needed to scrub and standardize street addresses exported from a legacy system before importing them into a newer system. (See, I don’t just make this stuff up; it’s actually useful.) This example shows how I approached the problem.

Press + to interact
s = '100 NORTH MAIN ROAD'
print(s.replace('ROAD', 'RD.')) #①
#'100 NORTH MAIN RD.'
s = '100 NORTH BROAD ROAD'
print(s.replace('ROAD', 'RD.')) #②
#'100 NORTH BRD. RD.'
print(s[:-4] + s[-4:].replace('ROAD', 'RD.')) #③
#'100 NORTH BROAD RD.'
import re #④
print(re.sub('ROAD$', 'RD.', s)) #⑤
#'100 NORTH BROAD RD.'

① My goal is to standardize a street address so that 'ROAD' is always abbreviated as 'RD.'. At first glance, I thought this was simple enough that I could just use the string method replace(). After all, all the data was already uppercase, so case mismatches ...

Access this course and 1400+ top-rated courses and projects.