How to replace a comma

Discussion in 'Python' started by Lad, Dec 18, 2006.

  1. Lad

    Lad Guest

    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.
     
    Lad, Dec 18, 2006
    #1
    1. Advertising

  2. Lad

    Peter Otten Guest

    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
     
    Peter Otten, Dec 18, 2006
    #2
    1. Advertising

  3. Lad

    Jon Clements Guest

    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.
     
    Jon Clements, Dec 18, 2006
    #3
  4. From: "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.


    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
     
    Hendrik van Rooyen, Dec 18, 2006
    #4
  5. Lad

    Lad Guest

    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
     
    Lad, Dec 18, 2006
    #5
  6. 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.


    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 <> -- http://www.craig-wood.com/nick
     
    Nick Craig-Wood, Dec 18, 2006
    #6
  7. Lad

    Duncan Booth Guest

    "Hendrik van Rooyen" <> wrote:

    > From: "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.

    >
    > 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
     
    Duncan Booth, Dec 18, 2006
    #7
  8. 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

    >
     
    Jussi Salmela, Dec 18, 2006
    #8
  9. Lad

    Lad Guest

    Nick Craig-Wood wrote:
    > 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.

    >
    > 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.
     
    Lad, Dec 18, 2006
    #9
  10. "Lad" <> 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
     
    Hendrik van Rooyen, Dec 19, 2006
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Blais
    Replies:
    1
    Views:
    387
    Bruno Desthuilliers
    Jun 27, 2006
  2. Greg Ewing
    Replies:
    2
    Views:
    352
    Dieter Maurer
    Jun 29, 2006
  3. Alun
    Replies:
    3
    Views:
    4,535
    Masudur
    Feb 18, 2008
  4. Kev Jackson

    regexp to replace comma in text

    Kev Jackson, Oct 6, 2005, in forum: Ruby
    Replies:
    3
    Views:
    148
    Robert Klemme
    Oct 6, 2005
  5. shapper

    Replace comma

    shapper, Nov 22, 2008, in forum: Javascript
    Replies:
    4
    Views:
    101
    Evertjan.
    Nov 22, 2008
Loading...

Share This Page