CSV: what is the problem?

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:ininitialize’
from /usr/local/lib/ruby/1.9.1/csv.rb:1356:in new' from /usr/local/lib/ruby/1.9.1/csv.rb:1356:inparse’
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:ininitialize’
from /usr/local/lib/ruby/1.9.1/csv.rb:1329:in open' from /usr/local/lib/ruby/1.9.1/csv.rb:1329:inopen’
from (irb):4
from /usr/local/bin/irb:12:in `’
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?

On Feb 17, 2010, at 2:47 PM, François Boone wrote:

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

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 => “,”)

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

Hope that helps.

James Edward G. II

James Edward G. II wrote:

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 G. 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:inopen’
from /usr/local/lib/ruby/1.9.1/csv.rb:1329:in open' from aipsa2.rb:63:in

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

Thank you for help

James Edward G. II wrote:

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

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

On Feb 17, 2010, at 3:57 PM, François Boone wrote:

/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:inopen’
from /usr/local/lib/ruby/1.9.1/csv.rb:1329:in open' from aipsa2.rb:63:in

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 => “;”) { |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 G. II

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs