Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python to transform street type abbreviation?

I am dealing with addresses matching such as 123 Main St. Suite 100 Chicago, IL and 123 Main Street Chicago, IL. One important problem is to transform abbreviations of street type, such as St. to Street. I wonder if there is any Python package that deals with it since it seems like a very common problem for dealing with addresses.

PS, I know usaddress, but it only parses out all the parts, does not do any transformation.

like image 982
Justin Li Avatar asked May 14 '26 16:05

Justin Li


1 Answers

You can convert to an abbreviation followed by a period by using address and usaddress. I used usaddress since I know it will parse addresses such as 123 S North Dr. This is part of what I did:

import usaddress
from address import AddressParser, Address
addr = usaddress.parse(address_line1)
ad = AddressParser()
addr2 = ad.parse_address(address_line1)
#perform some cleanup and functions on addr...
if addr2.street_suffix:
    post = addr2.street_suffix
else:
    post = ''

Here is the documentation on the address 0.1.1 module.

Update: address does not work in Python 3.x because there is a print function without (). There is another module, street-address documentation here that works similarly for formatting and parsing addresses, but I've found usaddress is sufficient.

For example,

    parser_address = "6400 S FIDDLERS GREEN CIR SUIT 123"
    addr = usaddress.parse(parser_address)

returns:

     [('6400', 'AddressNumber'), ('S', 'StreetNamePreDirectional'), ('FIDDLERS', 'StreetName'), ('GREEN', 'StreetName'), ('CIR', 'StreetNamePostType'), ('SUIT', 'OccupancyType'), ('123', 'OccupancyIdentifier')]
like image 156
Jessica Avatar answered May 16 '26 05:05

Jessica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!