Harrison Harrison - 1 year ago 47
Python Question

Add letters to string conditionally


1 10 avenue

Desired Output:
1 10th avenue

As you can see above I have given an example of an input, as well as the desired output that I would like. Essentially I need to look for instances where there is a number followed by a certain pattern (avenue, street, etc). I have a list which contains all of the patterns and it's called

If that number does not have "th" after it, I would like to add "th". Simply adding "th" is fine, because other portions of my code will correct it to either "st", "nd", "rd" if necessary.


1 10th avenue

1 10 avenue

I have implemented a working solution, which is this:

def Add_Th(address):
address = address.split(' ')
except AttributeError:
for pattern in patterns:
location = address.index(pattern) - 1
number_location = address[location]
except (ValueError, IndexError):
if 'th' not in number_location:
new = number_location + 'th'
address[location] = new
address = ' '.join(address)

return address

I would like to convert this implementation to regex, as this solution seems a bit messy to me, and occasionally causes some issues. I am not the best with regex, so if anyone could steer me in the right direction that would be greatly appreciated!

Here is my current attempt at the regex implementation:

def add_th(address):

find_num = re.compile(r'(?P<number>[\d]{1,2}(' + "|".join(patterns + ')(?P<following>.*)')
check_th = find_num.search(address)

if check_th is not None:
if re.match(r'(th)', check_th.group('following')):
return address
# this is where I would add th. I know I should use re.sub, i'm just not too sure
# how I would do it
return address

I do not have a lot of experience with regex, so please let me know if any of the work I've done is incorrect, as well as what would be the best way to add "th" to the appropriate spot.


Answer Source

Just one way, finding the positions behind a digit and ahead of one of those pattern words and placing 'th' into them:

>>> address = '1 10 avenue 3 33 street'
>>> patterns = ['avenue', 'street']
>>> import re
>>> pattern = re.compile(r'(?<=\d)(?= ({}))'.format('|'.join(patterns)))
>>> pattern.sub('th', address)
'1 10th avenue 3 33th street'