require ‘csv’
CSV.open(‘text.txt’, ‘r’).each do |person|
puts person.inspect
end
ruby “database.rb”
C:/Ruby192/lib/ruby/1.9.1/csv.rb:1892:in block (2 levels) in shift': Unquoted fields do not allow \r or \n (line 1). (CSV::MalformedCSVError) from C:/Ruby192/lib/ruby/1.9.1/csv.rb:1863:ineach’
from C:/Ruby192/lib/ruby/1.9.1/csv.rb:1863:in block in shift' from C:/Ruby192/lib/ruby/1.9.1/csv.rb:1825:inloop’
from C:/Ruby192/lib/ruby/1.9.1/csv.rb:1825:in shift' from C:/Ruby192/lib/ruby/1.9.1/csv.rb:1767:ineach’
from database.rb:2:in `’
Exit code: 1
C:/Ruby192/lib/ruby/1.9.1/csv.rb:1892:in block (2 levels) in shift': Unquoted fields do not allow \r or \n (line 1). (CSV::MalformedCSVError) from C:/Ruby192/lib/ruby/1.9.1/csv.rb:1863:ineach’
from C:/Ruby192/lib/ruby/1.9.1/csv.rb:1863:in block in shift' from C:/Ruby192/lib/ruby/1.9.1/csv.rb:1825:inloop’
from C:/Ruby192/lib/ruby/1.9.1/csv.rb:1825:in shift' from C:/Ruby192/lib/ruby/1.9.1/csv.rb:1767:ineach’
from database.rb:2:in `’
Yes, and it worked flawlessly. I’ve also checked the file, and there are
no “strange” characters in it (just normal Windows newlines: \r\n).
So it would be great if anyone had an explanation for this behaviour. I
remember a bug in Ruby concerning the newline transformation in “r” or
“w” mode. Maybe that’s the reason? My Ruby is on patch level 125.
Windoze new lines are stupid. Read up on the “b” file mode to see what
it does.
Can’t you just give a simple explanation here?
“Windoze new lines are stupid” is the simplest I can make it. But you’re
right, pointing you towards something to read/learn about probably
complicated things a bit much.
“Windoze new lines are stupid” is the simplest I can make it. But you’re
right, pointing you towards something to read/learn about probably
complicated things a bit much.
OK, it seems Ryan doesn’t know the reason either.
Does anyone else know what’s going on here? Is it a bug? Is it a problem
of Ruby or of the CSV parser?
Windows uses \r\n for newlines. Linux uses \n. Ruby’s CSV apparently
doesn’t support \r\n, only \n.
Opening files in “r” mode performs some conversion on the input, for
example encoding conversion or drum roll newline conversion. “rb”
(binary) mode suppresses them and yields precisely the same bytes as
present in the file.
“Windoze new lines are stupid” is the simplest I can make it. But you’re
right, pointing you towards something to read/learn about probably
complicated things a bit much.
OK, it seems Ryan doesn’t know the reason either.
And yet gasp my suggestion fixed his problem…
I don’t have (or want) a windoze box to demonstrate this.
“Windoze new lines are stupid” is the simplest I can make it. But you’re
right, pointing you towards something to read/learn about probably
complicated things a bit much.
OK, it seems Ryan doesn’t know the reason either.
Does anyone else know what’s going on here? Is it a bug? Is it a problem
of Ruby or of the CSV parser?
Windows uses \r\n for newlines. Linux uses \n. Ruby’s CSV apparently
doesn’t support \r\n, only \n.
Opening files in “r” mode performs some conversion on the input, for
example encoding conversion or drum roll newline conversion. “rb”
(binary) mode suppresses them and yields precisely the same bytes as
present in the file.
Well, but then you would expect the problem to occur in “rb” mode and
not in “r” mode. Because when all CRLF are converted into LF, they
should be just as good as “original” Linux newlines.
But it’s actually the other way round: While “rb” works, “r” doesn’t.
And it also seems to depend on the Ruby version (or version of the CSV
parser).
Ryan D. wrote in post #1066426:
And yet gasp my suggestion fixed his problem…
I don’t have (or want) a windoze box to demonstrate this.
On Jun 27, 2012, at 14:54 , Bartosz Dziewoński wrote:
Well then probably Ruby’s CSV sucks. I don’t know, never used it. I’m
too lazy to install 1.9.2 now and try to replicate.
CSV ships in both ruby 1.8 and 1.9… but they’re different impls. Ruby
1.9 ships with JEG2’s faster_csv impl and 1.8 ships with… whatever it
has always shipped with.
Well, but then you would expect the problem to occur in “rb” mode and
not in “r” mode. Because when all CRLF are converted into LF, they
should be just as good as “original” Linux newlines.
But it’s actually the other way round: While “rb” works, “r” doesn’t.
And it also seems to depend on the Ruby version (or version of the CSV
parser).
Well then probably Ruby’s CSV sucks. I don’t know, never used it. I’m
too lazy to install 1.9.2 now and try to replicate.