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

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 Mon, Sep 1, 2008 at 1:09 PM, Chris Lowis <chris.lowis@gmail.com> wrote:

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 ?

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 Gray II

···

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

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

Chris Lowis schrieb:

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 ?

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

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

···

On Sep 1, 6:10 am, Chris Lowis <chris.lo...@gmail.com> wrote:

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"]]

Chris Lowis schrieb:

> 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"]]

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, 8:34 am, Lars Broecker <Lars.Broec...@iais.fraunhofer.de> 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 ?

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 Gray 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?

···

--
Posted via http://www.ruby-forum.com/\.

[["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

Hope that helps.
James Edward Gray 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

···

--
Posted via http://www.ruby-forum.com/\.

clever. thx.

a @ http://codeforpeople.com/

···

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

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

--
we can deny everything, except that we have the possibility of being better. simply reflect on that.
h.h. the 14th dalai lama

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 Gray II

···

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

On Sep 2, 8:34 am, Lars Broecker <Lars.Broec...@iais.fraunhofer.de> > wrote:

Chris Lowis schrieb:

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"]]

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]

James Gray 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 Gray II

···

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

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 Gray II

···

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

Hope that helps.
James Edward Gray 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.

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 Gray II

···

On Sep 5, 2008, at 5:07 PM, James Gray wrote:

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

Hope that helps.
James Edward Gray 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.