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:in `each'
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:in `loop'
from C:/Ruby192/lib/ruby/1.9.1/csv.rb:1825:in `shift'
from C:/Ruby192/lib/ruby/1.9.1/csv.rb:1767:in `each'
from database.rb:2:in `<main>'
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:in `each'
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:in `loop'
from C:/Ruby192/lib/ruby/1.9.1/csv.rb:1825:in `shift'
from C:/Ruby192/lib/ruby/1.9.1/csv.rb:1767:in `each'
from database.rb:2:in `<main>'
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:in `each'
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:in `loop'
from C:/Ruby192/lib/ruby/1.9.1/csv.rb:1825:in `shift'
from C:/Ruby192/lib/ruby/1.9.1/csv.rb:1767:in `each'
from database.rb:2:in `<main>'
-----------------------------------------------
Fred Bloggs,Manager,Male,45
Laura Smith,Cook,Female,23
Debbie Watts,Professor,Female,38
-----------------------------------------------
The result is suppposed to look like this:
------------------------------------------------
["Fred Bloggs", "Manager", "Male", "45"]
["Laura Smith", "Cook", "Female", "23"]
["Debbie Watts", "Professor", "Female", "38"]
-----------------------------------------------
I'm on windows 7. The text file is in the same directory as the .rb
file.
Thank you!
Yes, and it worked flawlessly. I've also checked the file, and there are
no "strange" characters in it (just normal Windows newslines: \r\n).
So it would be great if anyone had an explanation for this behaviour. I
remember a bug in Ruby concerning the new line transformation in "r" or
"w" mode. Maybe that's the reason? My Ruby is on patch level 125.
The text file?
-----------------------------------------------
Fred Bloggs,Manager,Male,45
Laura Smith,Cook,Female,23
Debbie Watts,Professor,Female,38
-----------------------------------------------
It does work for me and yields the expected result (Ruby 1.9.3 on
Windows 7).
Can you *attach* the text file rather than post it? Maybe there are some
"hidden" newlines?
"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.
···
On Jun 27, 2012, at 04:00 , Kaye Ng wrote:
Ryan Davis wrote in post #1066299:
Windoze new lines are stupid. Read up on the "b" file mode to see what
it does.
"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.
-- Matma Rex
···
2012/6/27 Jan E. <lists@ruby-forum.com>:
Ryan Davis wrote in post #1066410:
"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?
I don't have (or want) a windoze box to demonstrate this.
···
On Jun 27, 2012, at 14:20 , Jan E. wrote:
Ryan Davis wrote in post #1066410:
"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.
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).
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.
-- Matma Rex
···
2012/6/27 Jan E. <lists@ruby-forum.com>:
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).
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.
···
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.