CSV: what is the problem ?

F

François Boone

Do you have any solution?


%which ruby
/usr/local/bin/ruby

%ruby --version
ruby 1.9.1p376 (2009-12-07 revision 26041) [i386-darwin10.2.0]

%irb
irb(main):001:0> require 'csv'
=> true
irb(main):002:0> a = '1,2,3'
=> "1,2,3"
irb(main):003:0> CSV.parse(a, ',')
TypeError: can't convert String into Hash
from /usr/local/lib/ruby/1.9.1/csv.rb:1548:in `merge'
from /usr/local/lib/ruby/1.9.1/csv.rb:1548:in `initialize'
from /usr/local/lib/ruby/1.9.1/csv.rb:1356:in `new'
from /usr/local/lib/ruby/1.9.1/csv.rb:1356:in `parse'
from (irb):3
from /usr/local/bin/irb:12:in `<main>'
irb(main):004:0> CSV.open('test.csv', 'r', ',') { |r| puts r }
TypeError: can't convert String into Integer
from /usr/local/lib/ruby/1.9.1/csv.rb:1329:in `initialize'
from /usr/local/lib/ruby/1.9.1/csv.rb:1329:in `open'
from /usr/local/lib/ruby/1.9.1/csv.rb:1329:in `open'
from (irb):4
from /usr/local/bin/irb:12:in `<main>'
irb(main):005:0>

Exactly like Ticket #460:
status changed from new to closed
resolution set to invalid

what does it mean: resolution set to invalid?
 
J

James Edward Gray II

irb(main):003:0> CSV.parse(a, ',')
TypeError: can't convert String into Hash
Exactly like Ticket #460:
status changed from new to closed
resolution set to invalid
=20
what does it mean: resolution set to invalid?

It means that in Ruby 1.9 the old CSV library was replaced by the =
FasterCSV code base. FasterCSV (now renamed to CSV in 1.9) has a =
slightly different interface. The call above would need to be =
translated to that new interface:

CSV.parse(a, :col_sep =3D> ",")

In this case though, it would be better to leave out the separator, =
since it is the default.

Hope that helps.

James Edward Gray II=
 
F

François Boone

James said:
It means that in Ruby 1.9 the old CSV library was replaced by the
FasterCSV code base. FasterCSV (now renamed to CSV in 1.9) has a
slightly different interface. The call above would need to be
translated to that new interface:

CSV.parse(a, :col_sep => ",")

In this case though, it would be better to leave out the separator,
since it is the default.

Hope that helps.

James Edward Gray II

Ok, I did not understand something!.
I have updated my ruby from 1.8.6 to 1.9.1

I used with ruby 1.8.6
FasterCSV.foreach(fichier, :col_sep =>';', :row_sep =>:auto) do |ligne|
where 'fichier' in the name of my file
and all is ok

Now, I have to change this line to use my program with ruby 1.9.1:
CSV.foreach(fichier, :col_sep => ";") do |ligne|
gives
/usr/local/lib/ruby/1.9.1/csv.rb:1329:in `initialize': can't convert
String into Integer (TypeError)
from /usr/local/lib/ruby/1.9.1/csv.rb:1329:in `open'
from /usr/local/lib/ruby/1.9.1/csv.rb:1329:in `open'
from aipsa2.rb:63:in `<main>'

I check http://ruby-doc.org/stdlib/libdoc/csv/rdoc/index.html
however no example is given with ";" separator.

Thank you for help
 
J

James Edward Gray II

I have updated my ruby from 1.8.6 to 1.9.1
=20
I used with ruby 1.8.6
FasterCSV.foreach(fichier, :col_sep =3D>';', :row_sep =3D>:auto) do = |ligne|
where 'fichier' in the name of my file
and all is ok
=20
Now, I have to change this line to use my program with ruby 1.9.1:
CSV.foreach(fichier, :col_sep =3D> ";") do |ligne|
gives
/usr/local/lib/ruby/1.9.1/csv.rb:1329:in `initialize': can't convert=20=
String into Integer (TypeError)
from /usr/local/lib/ruby/1.9.1/csv.rb:1329:in `open'
from /usr/local/lib/ruby/1.9.1/csv.rb:1329:in `open'
from aipsa2.rb:63:in `<main>'

Your example works fine for me with Ruby 1.9:

$ echo '1;2;3' > data.csv
$ ruby -v -r csv -e 'CSV.foreach(ARGV.shift, :col_sep =3D> ";") { |row| =
p row }' data.csv=20
ruby 1.9.2dev (2009-07-18) [i386-darwin10.0.0]
["1", "2", "3"]

Very little changed with CSV between 1.9.1 and 1.9.2 and I expect it to =
work on the lower version too.

James Edward Gray II
 
F

François Boone

James said:
On Feb 17, 2010, at 3:57 PM, Fran�ois Boone wrote:

$ echo '1;2;3' > data.csv
$ ruby -v -r csv -e 'CSV.foreach(ARGV.shift, :col_sep => ";") { |row| p
row }' data.csv
ruby 1.9.2dev (2009-07-18) [i386-darwin10.0.0]
["1", "2", "3"]

Very little changed with CSV between 1.9.1 and 1.9.2 and I expect it to
work on the lower version too.

James Edward Gray II

Yes, it works ! :)
I think it's time to take a break.
Thank you very much for your time
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top