hleggs hleggs - 3 months ago 7
Python Question

Remove numbers conditionally?

I'm sorry if the title isn't very descriptive. I don't exactly know how to sum up my problem in a few words.

Here's my issue. I'm cleaning addresses and some of them are causing some issues.

I have a list of delimiters (avenue, street, road, place, etc etc etc) named

patterns
.

Let's say I have this address for example:
SUITE 1603 200 PARK AVENUE SOUTH NEW YORK


I would like the output to be
SUITE 200 PARK AVENUE SOUTH NEW YORK


Is there any way I could somehow look to see if there are 2 batches of numbers (in this case
1603
and
200
) before one of my patterns and if so, strip the first batch of numbers from my string? i.e remove
1603
and keep
200
.

Update: I've added this line to my code:

address = re.sub("\d+", "", address)
however it's currently removing all the numbers. I thought that by putting ,1 after address it would only remove the first occurrence but that wasn't the case

Answer

You just need to specify you want to replace one occurrence:

>>> re.sub("\d+", "", 'SUITE 1603 200 PARK AVENUE SOUTH NEW YORK', 1)
                                                                   ^
'SUITE  200 PARK AVENUE SOUTH NEW YORK'
>>> 
Comments