CSV.open problem, help please

This code doesn't seem to work

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: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>'

Exit code: 1

Help please!

--
Posted via http://www.ruby-forum.com/\.

What does your csv file look like?

Sam

···

On 06/27/2012 06:14 PM, Kaye Ng wrote:

This code doesn't seem to work

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: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>'

Exit code: 1

Help please!

Try "rb" instead or "r"

···

On Jun 26, 2012, at 23:14, Kaye Ng <lists@ruby-forum.com> wrote:

This code doesn't seem to work

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: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>'

Exit code: 1

Help please!

--
Posted via http://www.ruby-forum.com/\.

Jan E.
Did you test the attached file?

···

--
Posted via http://www.ruby-forum.com/.

I'm on Ruby 1.9.2, maybe I should upgrade? I'm afraid I don't know what
patch level is.

···

--
Posted via http://www.ruby-forum.com/.

I tried the same code and csv file but, it works. I'm using ruby version
1.9.2 with patch level 318.

···

--
Posted via http://www.ruby-forum.com/.

What does your csv file look like?

Sam

The text file?

···

-----------------------------------------------
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!

--
Posted via http://www.ruby-forum.com/\.

Ryan Davis wrote in post #1066268:

Try "rb" instead or "r"

It worked Ryan. Would you mind explaining why?

···

--
Posted via http://www.ruby-forum.com/\.

Kaye Ng wrote in post #1066310:

Jan E.
Did you test the attached file?

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.

···

--
Posted via http://www.ruby-forum.com/\.

Hi,

Kaye Ng wrote in post #1066265:

What does your csv file look like?

Sam

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?

···

--
Posted via http://www.ruby-forum.com/\.

Windoze new lines are stupid. Read up on the "b" file mode to see what it does.

···

On Jun 27, 2012, at 0:24, Kaye Ng <lists@ruby-forum.com> wrote:

Ryan Davis wrote in post #1066268:

Try "rb" instead or "r"

It worked Ryan. Would you mind explaining why?

--
Posted via http://www.ruby-forum.com/\.

Can you *attach* the text file rather than post it? Maybe there are some

"hidden" newlines?

Here it is. Thank you.

Attachments:
http://www.ruby-forum.com/attachment/7530/text.txt

···

--
Posted via http://www.ruby-forum.com/\.

Ryan Davis wrote in post #1066299:

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?

···

--
Posted via http://www.ruby-forum.com/\.

"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.

Can't you just give a simple explanation here?

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?

···

--
Posted via http://www.ruby-forum.com/\.

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?

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: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.

OK, it seems Ryan doesn't know the reason either.

Bartosz Dziewoński wrote in post #1066424:

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).

···

--
Posted via http://www.ruby-forum.com/\.

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.