Strip is not stripping trailing whitespace

Discussion in 'Ruby' started by Taylor Strait, Dec 28, 2006.

  1. I have files with city names which have one or two trailing whitespaces:

    Adelanto <-
    Agoura Hills <-
    Alameda <-
    Albany <-
    Alhambra <-
    Aliso Viejo <-

    My method just iterates and strips!

    def trim(state)
    diskfile = File.new(state + "-cleaned.txt", "w")
    $stdout = diskfile

    IO.foreach(state + ".txt") do |line|
    line.strip!
    puts line
    end

    diskfile.close
    $stdout = STDOUT
    end

    The output successfully removes leading whitespace but not trailing
    whitespace. What am I doing wrong? I would chop! but the number of
    trailing whitespace characters varies and my attempt at a while loop to
    check and chop! was unsuccessful.

    --
    Posted via http://www.ruby-forum.com/.
    Taylor Strait, Dec 28, 2006
    #1
    1. Advertising

  2. Taylor Strait wrote:
    > I have files with city names which have one or two trailing whitespaces:
    >
    > Adelanto <-
    > Agoura Hills <-
    > Alameda <-
    > Albany <-
    > Alhambra <-
    > Aliso Viejo <-
    >
    > My method just iterates and strips!
    >
    > def trim(state)
    > diskfile = File.new(state + "-cleaned.txt", "w")
    > $stdout = diskfile
    >
    > IO.foreach(state + ".txt") do |line|
    > line.strip!
    > puts line
    > end
    >
    > diskfile.close
    > $stdout = STDOUT
    > end
    >
    > The output successfully removes leading whitespace but not trailing
    > whitespace. What am I doing wrong? I would chop! but the number of
    > trailing whitespace characters varies and my attempt at a while loop to
    > check and chop! was unsuccessful.
    >
    > --
    > Posted via http://www.ruby-forum.com/.


    Perhaps there are some control characters at the lines' ends.

    The way that you're writing to a file seems roundabout
    and peculiar to me.

    def trim(state)
    open(state + "-cleaned.txt", "w") do |out|
    IO.foreach(state + ".txt") do |line|
    # The next line will show control characters.
    p line
    out.puts line.strip
    end
    end
    end
    William James, Dec 28, 2006
    #2
    1. Advertising

  3. On Dec 28, 2006, at 1:29 AM, Taylor Strait wrote:

    > I have files with city names which have one or two trailing
    > whitespaces:
    >
    > Adelanto <-
    > Agoura Hills <-
    > Alameda <-
    > Albany <-
    > Alhambra <-
    > Aliso Viejo <-
    >
    > My method just iterates and strips!
    >
    > def trim(state)
    > diskfile = File.new(state + "-cleaned.txt", "w")
    > $stdout = diskfile
    >
    > IO.foreach(state + ".txt") do |line|
    > line.strip!
    > puts line
    > end
    >
    > diskfile.close
    > $stdout = STDOUT
    > end
    >
    > The output successfully removes leading whitespace but not trailing
    > whitespace. What am I doing wrong? I would chop! but the number of
    > trailing whitespace characters varies and my attempt at a while
    > loop to
    > check and chop! was unsuccessful.


    Strip is working as it should. Your input lines don't end is spaces,
    but with a line end code. The easy way to do what you want is
    something like the following:

    <code>
    #! /usr/bin/env ruby -w

    PREFOX = "/Users/mg/Desktop/test"
    SUFFIX = ".txt"
    File.open(PREFOX + "-cleaned" + SUFFIX, "w") do |out_file|
    File.open(PREFOX + SUFFIX) do |in_file|
    in_file.each { |line| out_file.puts line.chomp.strip }
    end
    end
    </code>

    Note the use of String#chomp. Also, I'm recommending that you use
    File.open and that you don't mess with $stdout. File#open
    automatically takes care of closing the files it opens.

    Regards, Morton
    Morton Goldberg, Dec 28, 2006
    #3
  4. def trim(state)
    open(state + "-cleaned.txt", "w") do |out|
    IO.foreach(state + ".txt") do |line|
    # The next line will show control characters.
    p line
    out.puts line.strip
    end
    end
    end

    outputs:

    "Yorba Linda\240\240\n"
    "Yountville \n"
    "Yreka\240\240\n"
    "Yuba City\240\240\n"
    "Yucaipa\240\240\n"
    "Yucca Valley "
    => nil

    but alas the generated file still has trailing whitespaces. What should
    I do to remove the \240 and \n? Is that not what strip! does?

    --
    Posted via http://www.ruby-forum.com/.
    Taylor Strait, Dec 28, 2006
    #4
  5. > Note the use of String#chomp

    Thanks, Morton. That was the key. I just appended two .chomp methods
    and it fixed it right up. I appreciate everyone's help!

    --
    Posted via http://www.ruby-forum.com/.
    Taylor Strait, Dec 28, 2006
    #5
  6. Taylor Strait wrote:
    > > Note the use of String#chomp

    >
    > Thanks, Morton. That was the key. I just appended two .chomp methods
    > and it fixed it right up.


    It couldn't have.

    strip removes newlines, because they are whitespace.
    On the other hand, neither chomp nor strip removes "\240".

    >> "foo \n".strip

    => "foo"
    >> ("foo " + 0240.chr).chomp

    => "foo \240"
    William James, Dec 28, 2006
    #6
  7. > It couldn't have.
    >
    > strip removes newlines, because they are whitespace.
    > On the other hand, neither chomp nor strip removes "\240".
    >
    >>> "foo \n".strip

    > => "foo"
    >>> ("foo " + 0240.chr).chomp

    > => "foo \240"


    This is why I shouldn't code at 3:30am! I had used chop instead, which
    of course truncated the text in rare cases. How can I remove \240?

    --
    Posted via http://www.ruby-forum.com/.
    Taylor Strait, Dec 28, 2006
    #7
  8. Taylor Strait

    Eric Hodel Guest

    On Dec 28, 2006, at 24:31, Taylor Strait wrote:
    >> It couldn't have.
    >>
    >> strip removes newlines, because they are whitespace.
    >> On the other hand, neither chomp nor strip removes "\240".
    >>
    >>>> "foo \n".strip

    >> => "foo"
    >>>> ("foo " + 0240.chr).chomp

    >> => "foo \240"

    >
    > This is why I shouldn't code at 3:30am! I had used chop instead,
    > which
    > of course truncated the text in rare cases. How can I remove \240?


    String#gsub

    line.gsub(/[\240]/, '')

    --
    Eric Hodel - - http://blog.segment7.net

    I LIT YOUR GEM ON FIRE!
    Eric Hodel, Dec 28, 2006
    #8
  9. Taylor Strait, Dec 28, 2006
    #9
  10. Taylor Strait

    Carlos Guest

    [Taylor Strait <>, 2006-12-28 09.31 CET]
    > > It couldn't have.
    > >
    > > strip removes newlines, because they are whitespace.
    > > On the other hand, neither chomp nor strip removes "\240".
    > >
    > >>> "foo \n".strip

    > > => "foo"
    > >>> ("foo " + 0240.chr).chomp

    > > => "foo \240"

    >
    > This is why I shouldn't code at 3:30am! I had used chop instead, which
    > of course truncated the text in rare cases. How can I remove \240?


    line.delete! "\240"

    or, if you want to delete them at the end only,

    line.sub! /\240+$/, ""

    Good luck.

    --
    Carlos, Dec 28, 2006
    #10
  11. Taylor Strait wrote:
    > > It couldn't have.
    > >
    > > strip removes newlines, because they are whitespace.
    > > On the other hand, neither chomp nor strip removes "\240".
    > >
    > >>> "foo \n".strip

    > > => "foo"
    > >>> ("foo " + 0240.chr).chomp

    > > => "foo \240"

    >
    > This is why I shouldn't code at 3:30am! I had used chop instead, which
    > of course truncated the text in rare cases. How can I remove \240?
    >
    > --
    > Posted via http://www.ruby-forum.com/.


    Remove ASCII 128-- 255 at end of line:

    irb(main):016:0> "foo\240\250 \n".strip.
    irb(main):017:0* sub(/[#{128.chr}-#{255.chr}]+$/,"")
    => "foo"
    William James, Dec 28, 2006
    #11
  12. On 28.12.2006 09:56, William James wrote:
    > Taylor Strait wrote:
    >>> It couldn't have.
    >>>
    >>> strip removes newlines, because they are whitespace.
    >>> On the other hand, neither chomp nor strip removes "\240".
    >>>
    >>>>> "foo \n".strip
    >>> => "foo"
    >>>>> ("foo " + 0240.chr).chomp
    >>> => "foo \240"

    >> This is why I shouldn't code at 3:30am! I had used chop instead, which
    >> of course truncated the text in rare cases. How can I remove \240?
    >>
    >> --
    >> Posted via http://www.ruby-forum.com/.

    >
    > Remove ASCII 128-- 255 at end of line:
    >
    > irb(main):016:0> "foo\240\250 \n".strip.
    > irb(main):017:0* sub(/[#{128.chr}-#{255.chr}]+$/,"")
    > => "foo"


    Why not combine them like this:

    irb(main):007:0> s="foo\240\250 \n"
    => "foo\240\250 \n"

    irb(main):015:0> s.sub /[\200-\377\s]+$/, ''
    => "foo"

    Cheers

    robert
    Robert Klemme, Dec 28, 2006
    #12
  13. On Thu, 2006-12-28 at 17:52 +0900, Eric Hodel wrote:
    > > This is why I shouldn't code at 3:30am! I had used chop instead,
    > > which
    > > of course truncated the text in rare cases. How can I remove \240?

    >
    > String#gsub
    >
    > line.gsub(/[\240]/, '')
    >
    >

    line.chop will remove one of the "\240"s. line.chop.chop.chop will
    remove all three, but they will still be on line. To completely remove
    them, use line.chop!.chop!.chop! where line = the string you wish to
    change.
    Charles A Gray, Dec 28, 2006
    #13
  14. Taylor Strait

    Chris Gernon Guest

    Charles A Gray wrote:
    > line.chop will remove one of the "\240"s. line.chop.chop.chop will
    > remove all three, but they will still be on line. To completely remove
    > them, use line.chop!.chop!.chop! where line = the string you wish to
    > change.


    line = line.chop.chop.chop would be better. In general, you shouldn't
    chain destructive methods, because they usually return nil when they
    fail:

    irb> line = ''
    => ""
    irb> line.chop!
    => nil
    irb> line = ''
    => ""
    irb(main):004:0> line.chop!chop!
    TypeError: $_ value need to be String (nil given)
    from (irb):4:in `chop!'
    from (irb):4

    As opposed to:

    irb> line = ''
    => ""
    irb> line = line.chop.chop.chop
    => ""

    --
    Posted via http://www.ruby-forum.com/.
    Chris Gernon, Dec 28, 2006
    #14
    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. Les Caudle
    Replies:
    0
    Views:
    598
    Les Caudle
    Mar 7, 2004
  2. KevinGPO
    Replies:
    1
    Views:
    329
    Richard Herring
    Feb 20, 2006
  3. Aquila
    Replies:
    35
    Views:
    437
    Mathieu Bouchard
    Mar 31, 2005
  4. Greg
    Replies:
    1
    Views:
    161
    Gunnar Hjalmarsson
    Jun 6, 2005
  5. yelipolok
    Replies:
    4
    Views:
    238
    John W. Krahn
    Jan 27, 2010
Loading...

Share This Page