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

Help please!

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

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

What does your csv file look like?

Sam

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!

Try “rb” instead or “r”

Ryan D. wrote in post #1066268:

Try “rb” instead or “r”

It worked Ryan. Would you mind explaining why?

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?

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

Can you attach the text file rather than post it? Maybe there are some
“hidden” newlines?

Here it is. Thank you.

Ryan D. 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?

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

Jan E.
Did you test the attached file?

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

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

On Jun 27, 2012, at 04:00 , Kaye Ng wrote:

Ryan D. 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?

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

2012/6/27 Jan E. [email protected]:

Ryan D. 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?

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

On Jun 27, 2012, at 14:20 , Jan E. wrote:

Ryan D. 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.

And yet gasp my suggestion fixed his problem…

I don’t have (or want) a windoze box to demonstrate this.

Ryan D. 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?

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

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.

Grow up.

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.

2012/6/27 Jan E. [email protected]:

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