[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Parsing a CSV file column-wise

Chris Lowis

9/1/2008 11:11:00 AM

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
11 Answers

Lars Broecker

9/2/2008 7:34:00 AM

0

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

Chris Lowis

9/2/2008 9:04:00 AM

0

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]


Thanks again,

Chris







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

9/2/2008 12:14:00 PM

0


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]

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

Lloyd Linklater

9/2/2008 1:41:00 PM

0

James Gray wrote:
> require "faster_csv"

Hiya James!

I always enjoy your posts. :)

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

--
Posted via http://www.ruby-....

James Gray

9/2/2008 1:45:00 PM

0

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

> James Gray wrote:
>> require "faster_csv"
>
> Hiya James!
>
> I always enjoy your posts. :)

Thanks.

> I have a quick question, if I may. What is the difference between
> faster_csv and fasterCSV (http://fastercsv.ruby...) 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

William James

9/3/2008 3:24:00 AM

0

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

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

Chris Lowis

9/3/2008 11:58:00 AM

0

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

Chris Lowis

9/4/2008 10:38:00 AM

0

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

ara.t.howard

9/4/2008 6:50:00 PM

0


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



clever. thx.

a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




James Gray

9/5/2008 10:07:00 PM

0

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