Re: Remove Whitespace

Discussion in 'Python' started by Kelvie Wong, Apr 13, 2006.

  1. Kelvie Wong

    Kelvie Wong Guest

    try this:

    string = 'D c a V e r " = d w o r d : 0 0 0 0 0 6 4 0'
    import re
    re.sub("\s", "", string)

    On 4/13/06, david brochu jr <> wrote:
    >
    > Hi again,
    >
    > Trying to remove whitespace from a string in a text file.
    >
    > the string is:
    > D c a V e r " = d w o r d : 0 0 0 0 0 6 4 0
    >
    > how would i go about doing this?
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    >
     
    Kelvie Wong, Apr 13, 2006
    #1
    1. Advertising

  2. Kelvie Wong

    Guest

    re.sub() doesn't do the substitution in place: it returns the resulting
    string. Try this:

    myString = 'D c a V e r " = d w o r d : 0 0 0 0 0 6 4 0'
    import re
    newString = re.sub("\s", "", myString)
    print newString
     
    , Apr 13, 2006
    #2
    1. Advertising

  3. On Thu, 13 Apr 2006 12:09:32 -0700, Kelvie Wong wrote:

    > try this:
    >
    > string = 'D c a V e r " = d w o r d : 0 0 0 0 0 6 4 0'
    > import re
    > re.sub("\s", "", string)


    Why would you want to call in the heavy sledgehammer of regular
    expressions for cracking this peanut?

    old_s = 'D c a V e r " = d w o r d : 0 0 0 0 0 6 4 0'
    new_s = old_s.replace(" ", "")

    And if you want to remove all whitespace:

    new_s = "".join(old_s.split())



    --
    Steven.
     
    Steven D'Aprano, Apr 14, 2006
    #3
  4. Em Sex, 2006-04-14 às 12:46 +1000, Steven D'Aprano escreveu:
    > Why would you want to call in the heavy sledgehammer of regular
    > expressions for cracking this peanut?


    And put heavy on that!

    $ python2.4 -mtimeit -s "str = 'D c a V e r \" = d w o r d : 0 0 0 0 0 6
    4 0'" 'str.replace(" ", "")'
    100000 loops, best of 3: 3.07 usec per loop
    $ python2.4 -mtimeit -s "str = 'D c a V e r \" = d w o r d : 0 0 0 0 0 6
    4 0'" '"".join(str.split())'
    100000 loops, best of 3: 4.16 usec per loop
    $ python2.4 -mtimeit -s "from re import sub; str = 'D c a V e r \" = d w
    o r d : 0 0 0 0 0 6 4 0'" 'sub("\\s", "", str)'
    10000 loops, best of 3: 23.6 usec per loop
    $ calc 23.6 / 3.07
    ~7.68729641693811074919

    I couldn't be expressed better:

    "Some people, when confronted with a problem, think "I know, I'll use
    regular expressions." Now they have two problems."
    — Jamie Zawinski, in comp.lang.emacs

    --
    Felipe.
     
    Felipe Almeida Lessa, Apr 14, 2006
    #4
  5. Kelvie Wong

    Martin Blais Guest

    On 13 Apr 2006 12:32:56 -0700,
    <> wrote:
    > re.sub() doesn't do the substitution in place: it returns the resulting
    > string. Try this:


    In-place substitution is impossible in Python, strings are immutable.
     
    Martin Blais, Apr 14, 2006
    #5
  6. Kelvie Wong

    John Machin Guest

    On 14/04/2006 12:51 PM, Felipe Almeida Lessa wrote:
    > Em Sex, 2006-04-14 às 12:46 +1000, Steven D'Aprano escreveu:
    >> Why would you want to call in the heavy sledgehammer of regular
    >> expressions for cracking this peanut?

    >
    > And put heavy on that!
    >
    > $ python2.4 -mtimeit -s "str = 'D c a V e r \" = d w o r d : 0 0 0 0 0 6
    > 4 0'" 'str.replace(" ", "")'


    Oi! The OP mentioned "whitespace" ...

    > 100000 loops, best of 3: 3.07 usec per loop
    > $ python2.4 -mtimeit -s "str = 'D c a V e r \" = d w o r d : 0 0 0 0 0 6
    > 4 0'" '"".join(str.split())'
    > 100000 loops, best of 3: 4.16 usec per loop
    > $ python2.4 -mtimeit -s "from re import sub; str = 'D c a V e r \" = d w
    > o r d : 0 0 0 0 0 6 4 0'" 'sub("\\s", "", str)'
    > 10000 loops, best of 3: 23.6 usec per loop
    > $ calc 23.6 / 3.07
    > ~7.68729641693811074919
    >
    > I couldn't be expressed better:
    >
    > "Some people, when confronted with a problem, think "I know, I'll use
    > regular expressions." Now they have two problems."
    > — Jamie Zawinski, in comp.lang.emacs
    >



    C:\junk>python -mtimeit -s "str = 23 * ' X'" "str.replace(' ', '')
    100000 loops, best of 3: 3.65 usec per loop

    C:\junk>python -mtimeit -s "str = 23 * ' X'" "str.replace(' ',
    '').replace('\t', '')
    100000 loops, best of 3: 4.33 usec per loop

    C:\junk>python -mtimeit -s "str = 23 * ' X'; cmap = ''.join(chr(k) for k
    in range(256)); delchars = ' \t'" "str.translate(cmap, delchars)"
    1000000 loops, best of 3: 0.883 usec per loop

    0.883 / 3.65 -> 0.242

    Some people, in Gadarene flight from regular expressions, don't read far
    enough in the Library Reference Manual :)
     
    John Machin, Apr 14, 2006
    #6
  7. John Machin wrote:

    > > $ python2.4 -mtimeit -s "str = 'D c a V e r \" = d w o r d : 0 0 0 0 0 6
    > > 4 0'" 'str.replace(" ", "")'

    >
    > Oi! The OP mentioned "whitespace" ...


    yeah. but as is obvious from his examples, he really means "UTF-16", not
    whitespace.

    </F>
     
    Fredrik Lundh, Apr 14, 2006
    #7
  8. Fredrik Lundh schreef:
    > John Machin wrote:
    >
    >>> $ python2.4 -mtimeit -s "str = 'D c a V e r \" = d w o r d : 0 0 0 0 0 6
    >>> 4 0'" 'str.replace(" ", "")'

    >> Oi! The OP mentioned "whitespace" ...

    >
    > yeah. but as is obvious from his examples, he really means "UTF-16", not
    > whitespace.


    Yes, that's what I was thinking. His data looks like a part of a Windows
    registry export, which at least on my system is in what Windows calls
    Unicode.

    --
    If I have been able to see further, it was only because I stood
    on the shoulders of giants. -- Isaac Newton

    Roel Schroeven
     
    Roel Schroeven, Apr 14, 2006
    #8
    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. threecrans
    Replies:
    0
    Views:
    1,468
    threecrans
    Jul 3, 2003
  2. Ryan Stewart
    Replies:
    11
    Views:
    5,898
    Roedy Green
    Jun 10, 2004
  3. Oli Filth
    Replies:
    9
    Views:
    3,365
    Uncle Pirate
    Jan 17, 2005
  4. Replies:
    10
    Views:
    798
    Eric Brunel
    Dec 16, 2008
  5. MRAB
    Replies:
    3
    Views:
    406
Loading...

Share This Page