Parsing a CSV file column-wise

Is there a short-cut to parsing a CSV file column-wise using any of
the available Ruby CSV libraries ?

For example, at the moment I have:

require ‘csv’
data = “1,2,3\n4,5,6\n7,8,9”
CSV::parse(data) # => [[“1”, “2”, “3”], [“4”, “5”, “6”], [“7”, “8”,
“9”]]

But what I’d like is:

CSV::parse(data,‘columnwise’) # => [[“1”, “4”, “7”], [“2”, “5”,
“8”], [“3”, “6”, “9”]]

Perhaps I just need to run the returned array through some kind of
transformation ?

Chris

On Mon, Sep 1, 2008 at 1:09 PM, Chris L. [email protected]
wrote:

But what I’d like is:

CSV::parse(data,‘columnwise’) # => [[“1”, “4”, “7”], [“2”, “5”,
“8”], [“3”, “6”, “9”]]

Perhaps I just need to run the returned array through some kind of
transformation ?

Maybe this helps:

irb(main):001:0> [[“1”, “2”, “3”], [“4”, “5”, “6”], [“7”, “8”,
irb(main):002:2* “9”]].transpose
=> [[“1”, “4”, “7”], [“2”, “5”, “8”], [“3”, “6”, “9”]]

Jesus.

On Sep 1, 2008, at 6:09 AM, Chris L. wrote:

Is there a short-cut to parsing a CSV file column-wise using any of
the available Ruby CSV libraries ?

Yes:

#!/usr/bin/env ruby -wKU

require “rubygems”
require “faster_csv”

data = “1,2,3\n4,5,6\n7,8,9”

table = FCSV.parse( data, :headers => true,
:return_headers => true )
table.by_col!
table[0] # => [“1”, “4”, “7”]
table[1] # => [“2”, “5”, “8”]
table[2] # => [“3”, “6”, “9”]

END

Hope that helps.

James Edward G. II

On Sep 2, 8:34 am, Lars B. [email protected]
wrote:

data = “1,2,3\n4,5,6\n7,8,9”
CSV::parse(data) # => [[“1”, “2”, “3”], [“4”, “5”, “6”], [“7”, “8”,
“9”]]

Lars,

That’s great ! To summarise for anyone else looking for an answer :

Given a CSV file tmp.csv

one,two,three
1,2,3
4,5,6
7,8,9

Where the first row contains the column headers

require ‘fastercsv’
table = FasterCSV::table(’./tmp.csv’)

table.by_col[2] # => [3, 6, 9]

Thanks again,

Chris

On Sep 2, 2008, at 3:59 AM, Chris L. wrote:

Given a CSV file tmp.csv

table.by_col[2] # => [3, 6, 9]

Or you could access them by name:

#!/usr/bin/env ruby -wKU

require “rubygems”
require “faster_csv”

csv = FCSV.parse(<<END_DATA, :headers => true, :header_converters
=> :symbol)
one,two,three
1,2,3
4,5,6
7,8,9
END_DATA
csv[:two] # => [“2”, “5”, “8”]

END

James Edward G. II

Chris L. schrieb:

But what I’d like is:

CSV::parse(data,‘columnwise’) # => [[“1”, “4”, “7”], [“2”, “5”,
“8”], [“3”, “6”, “9”]]

Perhaps I just need to run the returned array through some kind of
transformation ?

Take a look at FasterCSV. It is available via rubygems and allows
different modes in which to work on the parsed csv including working on
columns of csv files. Great library!

HTH,
Lars

James,

I was going to install and play with fasterCSV. Installing it into
eclipse was straightforward. Installing for JRuby in Netbeans did not
work as expected. Is there a trick to installing there?

James G. wrote:

require “faster_csv”

Hiya James!

I always enjoy your posts. :slight_smile:

I have a quick question, if I may. What is the difference between
faster_csv and fasterCSV (http://fastercsv.rubyforge.org/) if any?

On Sep 2, 2008, at 8:40 AM, Lloyd L. wrote:

James G. wrote:

require “faster_csv”

Hiya James!

I always enjoy your posts. :slight_smile:

Thanks.

I have a quick question, if I may. What is the difference between
faster_csv and fasterCSV (http://fastercsv.rubyforge.org/) if any?

The name of the library is FasterCSV. The gem you install is called
fastercsv though. (I’m not totally sure why I did that. Sorry!) You
require faster_csv (though fastercsv also works). It’s all the same
library, just different ways to reference it.

Hope that helps.

James Edward G. II

On Sep 1, 6:10 am, Chris L. [email protected] wrote:

But what I’d like is:

CSV::parse(data,‘columnwise’) # => [[“1”, “4”, “7”], [“2”, “5”,
“8”], [“3”, “6”, “9”]]

[[“1”, “2”, “3”], [“4”, “5”, “6”], [“7”, “8”, “9”]].transpose
==>[[“1”, “4”, “7”], [“2”, “5”, “8”], [“3”, “6”, “9”]]

Hope that helps.
James Edward G. II

Thank you for your help James, I’m sorry - I hadn’t noticed your replies
before, normally I read the group at google groups where all your
responses seem not to be mirrored. I happened to visit ruby-forum and
noticed them in the end. Not sure where the problem lies, if anywhere.

Chris

[[“1”, “2”, “3”], [“4”, “5”, “6”], [“7”, “8”, “9”]].transpose
==>[[“1”, “4”, “7”], [“2”, “5”, “8”], [“3”, “6”, “9”]]

That’s a very simple way to achieve what I want - thank you !

Chris

On Sep 4, 2008, at 5:37 AM, Chris L. wrote:

Hope that helps.
James Edward G. II

Thank you for your help James, I’m sorry - I hadn’t noticed your
replies
before, normally I read the group at google groups where all your
responses seem not to be mirrored.

It has been brought to my attention that the gateway is misbehaving.
I’m away at the LSRC right now, and thus may not be able to fix it.
I’ll look into this though and hopefully have it restored next week at
the latest. Sorry about the down time.

James Edward G. II

On Sep 2, 2008, at 9:19 PM, William J. wrote:

[[“1”, “2”, “3”], [“4”, “5”, “6”], [“7”, “8”, “9”]].transpose
==>[[“1”, “4”, “7”], [“2”, “5”, “8”], [“3”, “6”, “9”]]

clever. thx.

a @ http://codeforpeople.com/

On Sep 5, 2008, at 5:07 PM, James G. wrote:

It has been brought to my attention that the gateway is misbehaving.
This was my fault. I tried to upgrade TMail on the gateway server and
botched it. Then I went to a conference so I couldn’t fix it, of
course. :slight_smile:

Anyway, I believe the gateway will be fixed going forward now. Let me
know if you notice further issues though.

Sorry for the hassle.

James Edward G. II