How to replace a comma
Duncan Booth
duncan.booth at invalid.invalid
Mon Dec 18 10:42:44 EST 2006
"Hendrik van Rooyen" <mail at microcorp.co.za> wrote:
> From: "Lad" <python at hope.cz> wrote:
>
>
>> In a text I need to
>> add a blank(space) after a comma but only if there was no blank(space)
>> after the comman
>> If there was a blank(space), no change is made.
>>
>> I think it could be a task for regular expression but can not figure
>> out the correct regular expression.
>
> re's are a pain. Do this instead:
>
>>>> s = "hello, goodbye,boo"
>>>> s.replace(', ',',')
> 'hello,goodbye,boo'
>>>> _.replace(',',', ')
> 'hello, goodbye, boo'
>>>>
>
Personally I'd go one step further and regularise the whitespace around the
commas completely (otherwise what if you have spaces before commas, or
multiple spaces after them:
>>> s = "hello, goodbye , boo"
>>> print ', '.join(t.strip() for t in s.split(','))
hello, goodbye, boo
More information about the Python-list
mailing list