How to replace a comma
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. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to replace a comma
Lad 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. s = alpha, beta,gamma, delta , .join(t.replace(,, , ) for t in s.split(, )) 'alpha, beta, gamma, delta' Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: How to replace a comma
Lad 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. 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to replace a comma
From: Lad [EMAIL PROTECTED] 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' Hope this helps - Hendrik -- http://mail.python.org/mailman/listinfo/python-list
Re: How to replace a comma
Thank you for ALL for help. Hendrik, your solution works great but what is `_` in _.replace(',',', ') for? Thank you La. re's are a pain. Do this instead: s = hello, goodbye,boo s.replace(', ',',') 'hello,goodbye,boo' _.replace(',',', ') 'hello, goodbye, boo' Hope this helps - Hendrik -- http://mail.python.org/mailman/listinfo/python-list
Re: How to replace a comma
Lad [EMAIL PROTECTED] 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. You can do it with a zero width negative lookahead assertion, eg import re s=One, Two,Three,Four, File re.sub(r,(?!\s), , , s) 'One, Two, Three, Four, File' 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 Craig-Wood [EMAIL PROTECTED] -- http://www.craig-wood.com/nick -- http://mail.python.org/mailman/listinfo/python-list
Re: How to replace a comma
Hendrik van Rooyen [EMAIL PROTECTED] wrote: From: Lad [EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: How to replace a comma
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 Thank you La. re's are a pain. Do this instead: s = hello, goodbye,boo s.replace(', ',',') 'hello,goodbye,boo' _.replace(',',', ') 'hello, goodbye, boo' Hope this helps - Hendrik -- http://mail.python.org/mailman/listinfo/python-list
Re: How to replace a comma
Nick Craig-Wood wrote: Lad [EMAIL PROTECTED] 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. You can do it with a zero width negative lookahead assertion, eg import re s=One, Two,Three,Four, File re.sub(r,(?!\s), , , s) 'One, Two, Three, Four, File' 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to replace a comma
Lad [EMAIL PROTECTED] top posted: top posting fixed re's are a pain. Do this instead: s = hello, goodbye,boo s.replace(', ',',') 'hello,goodbye,boo' _.replace(',',', ') 'hello, goodbye, boo' 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 -- http://mail.python.org/mailman/listinfo/python-list