473,327 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

How to replace a comma

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.

Dec 18 '06 #1
9 9175
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
Dec 18 '06 #2

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.

Dec 18 '06 #3
From: "Lad" <py****@hope.czwrote:

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

Dec 18 '06 #4
Lad


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
Dec 18 '06 #5
Lad <py****@hope.czwrote:
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 <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Dec 18 '06 #6
"Hendrik van Rooyen" <ma**@microcorp.co.zawrote:
From: "Lad" <py****@hope.czwrote:

>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
Dec 18 '06 #7
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
Dec 18 '06 #8
Lad

Nick Craig-Wood wrote:
Lad <py****@hope.czwrote:
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.

Dec 18 '06 #9

"Lad" <py****@hope.cztop 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

Dec 19 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: John Smith | last post by:
Hi, Just wanted to check that I'm doing things the right way.......... I need to create a string along the lines of "23,43,78,23" with no comma at the beginning or end of it. The data comes...
3
by: Jim | last post by:
Hey all, I have a dilemma. I have a field in a table called "Supervisor." The table currently has 1,500+ records. Sometimes, the HR download that populates the field formats the field one way,...
6
by: JJ | last post by:
Hi all, I've got a very strange problem: I want to convert the commas in strings to dots. This is the function I wrote for this action: private string ddot(double input){ string str =...
18
by: james | last post by:
Hi, I am loading a CSV file ( Comma Seperated Value) into a Richtext box. I have a routine that splits the data up when it hits the "," and then copies the results into a listbox. The data also...
11
by: jarod1701 | last post by:
Hi, i'm currently trying to replace an unknown string using regular expressions. For example I have: user_pref("network.proxy.http", "server1") What do I have to do to replace the...
4
by: vvenk | last post by:
Hello: I have a string, "Testing_!@#$%^&*()". It may have single and double quotations as well. I would like to strip all chararcters others than a-z, A-Z, 0-9 and the comma. I came across...
10
by: vabby | last post by:
Hey guys. I have a problem with my code, I can replace comma for a dot here. "var tvn = parseFloat(document.getElementById('ctl00_ContentPlaceHolder1_txtMillilTVNHraefni').value.replace(/,/g,...
14
by: Adrienne Boswell | last post by:
Although this is a client side issue, I am also posting to asp.general in case there is someway to do this only server side (which I would prefer). Here's my form: <form method="post"...
4
by: ladymands | last post by:
I have a field address_line1 in a table called test, within this field I have commas' at various places, I have set a query to find these but when I use the update query to replace the comma's the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.