How to replace a comma

L

Lad

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.
Can anyone help, please?
Thank you
L.
 
P

Peter Otten

Lad said:
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.
'alpha, beta, gamma, delta'

Peter
 
J

Jon Clements

Lad said:
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.
Can anyone help, please?
Thank you
L.

Off the top of my head, something like re.sub(', *', ', ', 'a,
b,c,d,e, f'), meets your requirements (it also ensures the number of
spaces after the comma is one). However, you may need to refine the
rules depending on what you really want to achieve. For instance, what
happens with: a comma appearing before any text, consecutive commas (ie
,,,), or commas within quotes?

hth
Jon.
 
H

Hendrik van Rooyen

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:

Hope this helps - Hendrik
 
L

Lad

Thank you for ALL for help.
Hendrik,
your solution works great but
what is `_` in
_.replace(',',', ')

for?
Thank you
La.
 
N

Nick Craig-Wood

Lad said:
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.

You can do it with a zero width negative lookahead assertion, eg

From the help :-

(?!...)
Matches if ... doesn't match next. This is a negative lookahead
assertion. For example, Isaac (?!Asimov) will match 'Isaac ' only if
it's not followed by 'Asimov'

Or in a more straightforward but less efficient and accurate style -
this matches the next character which gets added back into the string.
>>> re.sub(r",([^\s])", r", \1", s) 'One, Two, Three, Four, File'
>>>

This shows a fundamental difference between the two methods
>>> t = ",,,,,"
>>> re.sub(r",(?!\s)", ", ", t) ', , , , , '
>>> re.sub(r",([^\s])", r", \1", t) ', ,, ,,'
>>>
 
D

Duncan Booth

Hendrik van Rooyen said:
re's are a pain. Do this instead:
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:
hello, goodbye, boo
 
J

Jussi Salmela

Lad kirjoitti:
Thank you for ALL for help.
Hendrik,
your solution works great but
what is `_` in
_.replace(',',', ')

for?
When you are trying things out in the Python shell IDLE, _ is a
shorthand way to use the last value printed by IDLE.

Thus when
s.replace(', ',',')
prints out
'hello,goodbye,boo'
_ refers to that value so the replace operation
_.replace(',',', ')

manipulates that value.

Cheers
Jussi
 
L

Lad

Nick said:
Lad said:
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.

You can do it with a zero width negative lookahead assertion, eg

From the help :-

(?!...)
Matches if ... doesn't match next. This is a negative lookahead
assertion. For example, Isaac (?!Asimov) will match 'Isaac ' only if
it's not followed by 'Asimov'

Or in a more straightforward but less efficient and accurate style -
this matches the next character which gets added back into the string.
re.sub(r",([^\s])", r", \1", s) 'One, Two, Three, Four, File'

This shows a fundamental difference between the two methods
t = ",,,,,"
re.sub(r",(?!\s)", ", ", t) ', , , , , '
re.sub(r",([^\s])", r", \1", t) ', ,, ,,'
Nick,
Thanks a lot.It works GREAT!
La.
 
H

Hendrik van Rooyen

"Lad" <[email protected]> top posted:

Thank you for ALL for help.
Hendrik,
your solution works great but
what is `_` in
_.replace(',',', ')

for?

The underscore, in the interactive interpreter, stands for
the result of the last statement. So in the above, you can
think of it as the "name" of the string without spaces after
the commas, on the previous line.

Note that to fix pathological stuff like:

s = "asasd, oipuopioiu, this is bad shit,\tiuiuiu,,, , "

you will have to do more work than the simple solution
above...

I am constantly amazed by how different we all are, when
I read the diverse solutions to such a seemingly simple
problem - and the nice thing is that they all work...

Have a good look at Peter Otten's solution - try to
understand it - it uses most of the very useful Python
functions.


- Hendrik
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top