Parsing a CSV file column-wise

C

Chris Lowis

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
 
C

Chris Lowis

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
 
J

James Gray

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
 
J

James Gray

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

William James

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

Chris Lowis

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

Chris Lowis

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
 
J

James Gray

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
 
J

James Gray

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top