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.
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.
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
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
···
On Dec 28, 2006, at 1:29 AM, Taylor Strait wrote:
I have files with city names which have one or two trailing whitespaces:
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.
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
> 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?
> 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.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.
···
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?
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:
line = ''
=> ""
line.chop!
=> nil
line = ''
=> ""
irb(main):004:0> line.chop!chop!
TypeError: $_ value need to be String (nil given)
from (irb):4:in `chop!'
from (irb):4