Q:

Normalizing street addresses in Django/Python

I have a Django form where one of the fields is a TextInput for a street address.
I want to normalize the data. For example:
>> normalize('420 East 24th St.')
'420 E. 24th Street'

>> normalize('221 Amsterdam Av')
'221 Amsterdam Ave.'

>> normalize('221 Amsterdam Avenue')
'221 Amsterdam Ave.'

Or something like that. I'm already using geopy for geocoding. Perhaps this might help?
Also: Where should I normalize? In the database model or in the clean function of the form field?

A:

The most reliable way to do this is to utilize a bona-fide address verification service.  Not only will it standardize (normalize) the address components according to USPS standards (see Publication 28) but you will also be certain that the address is real.  
Full disclosure:  I work for SmartyStreets, which provides just such a service.  Here's some really simple python sample code that shows how to use our service via an HTTP GET request:
https://github.com/smartystreets/LiveAddressSamples/blob/master/python/street-address.py

A:

I have recently created a street-address python module, and its StreetAddressFormatter can be used to normalize your address. 

A:

This is how I ended up addressing this (no pun intended):
### models.py ###

def normalize_address_for_display(address):

    display_address = string.capwords(address)

    # Normalize Avenue
    display_address = re.sub(r'\b(Avenue|Ave.)\b', 'Ave', display_address)

    # Normalize Street
    display_address = re.sub(r'\b(Street|St.)\b', 'St', display_address)

    # ...and other rules...

    return display_address

class Store(models.Model):

    name = models.CharField(max_length=32)
    address = models.CharField(max_length=64)
    city = models.CharField(max_length=32)
    state = models.CharField(max_length=2)
    zipcode = models.CharField(max_length=5)

    @property
    def display_address(self):
        return normalize_address_for_display(self.address)

I then use Place.display_address in templates. This allows me to keep the original user submitted data in the database without modification and just use display_address when I want a normalized display version.
Open for comments/suggestions.

