Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #7144

Re: How do i strip CSV's "Cell" designation when migrating to YAML

Newsgroups comp.lang.ruby
Date 2015-10-02 08:55 -0700
References <374b5041-0d3d-4c15-a3f1-d19d49d1f1f1@googlegroups.com> <43c1f51a-a977-4030-83df-9feaca285513@googlegroups.com>
Message-ID <f79e57b2-640b-453c-8b2c-8c88ffa33161@googlegroups.com> (permalink)
Subject Re: How do i strip CSV's "Cell" designation when migrating to YAML
From samtreweek@gmail.com

Show all headers | View raw


On Friday, 21 June 2013 14:50:22 UTC+1, lfas...@gmail.com  wrote:
> RESOLVED (as I was drifting off to sleep last night)
> The magic is in each element of the CSV record.
> 
> puts row[0].class  => CSV::Cell
> 
> so, replacing this line fixes the problem
> bad:  fleetH[k] = v 
> good: fleetH[k.to_s] = v.to_s

Just for kicks! - I had a similar issue with 1.8.7: hash doesn't work at all so used a bit of hackery

require 'csv'
require 'yaml'

csv  = CSV.parse("test.csv", :headers => true)
data = csv.each_with_index.map do |row, index|
  clean_row = row[0].split(",").map {|r| r.to_s}
  if index == 0
    @header = clean_row
    next
  end
  Hash[@header.zip(clean_row)]
end

p data.to_yaml

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

How do i strip CSV's "Cell" designation when migrating to YAML lfast1234@gmail.com - 2013-06-20 17:47 -0700
  Re: How do i strip CSV's "Cell" designation when migrating to YAML lfast1234@gmail.com - 2013-06-21 06:50 -0700
    Re: How do i strip CSV's "Cell" designation when migrating to YAML samtreweek@gmail.com - 2015-10-02 08:55 -0700
      Re: How do i strip CSV's "Cell" designation when migrating to YAML "WJ" <w_a_x_man@yahoo.com> - 2015-10-14 13:50 +0000

csiph-web