'How to remove whitespace from string if it is followed by certain character?

I need to remove a character from a string if it is followed by a certain character. In my case, I need to remove the whitespace if it is followed by a "/".

E.g.:

'... .. -- .--. .-.. . /.. ... /-... . - - . .-. /- .... .- -. /-.-. --- -- .--. .-.. . -..- .-.-.-'

The output needs to be:

'... .. -- .--. .-.. ./.. .../-... . - - . .-./- .... .- -./-.-. --- -- .--. .-.. . -..- .-.-.-'

I tried this with this code:

for i in range(1,(len(resultaat)-1)):
    kar1 = resultaat[i-1]
    spatie = resultaat[i]
    kar2 = resultaat[i+1]
    if (kar1 == "-" or kar1 == ".") and spatie == " " and kar2 == "/":
        resultaat = resultaat.rsplit(resultaat[i])

But I get this error:

IndexError: list index out of range

Can someone help me with this? Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source