Stuck on a three word street name regex

Brian D briandenzer at gmail.com
Thu Jan 28 18:36:52 CET 2010


> Correction:
>
> [snip] the expression "parts[1 : -1]" means gather list items from the
> second element in the list (index value 1) to one index position
> before the end of the list. [snip]

MRAB's solution was deserving of a more complete solution:

>>> def parse_address(address):
	# Handles poorly-formatted addresses:
	# 100 RAMPART S ST -- direction in wrong position
	# 45 JOHN CHURCHILL CHASE  ST -- two spaces before type
	#addresslist = ['num', 'dir', 'name', 'type']
	addresslist = ['', '', '', '']
	parts = address.split()
	if parts[-2] in ('E', 'W', 'N', 'S'):
		addresslist[1] = parts[-2]
		addresslist[2] = ' '.join(parts[1 : -2])
	else:
		addresslist[2] = ' '.join(parts[1 : -1])
	addresslist[0] = parts[0]
	addresslist[3] = parts[-1]
	return addresslist

>>> parse_address('45 John Churchill Chase N St')
['45', 'N', 'John Churchill Chase', 'St']
>>> parse_address('45 John Churchill Chase  St')
['45', '', 'John Churchill Chase', 'St']



More information about the Python-list mailing list