Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Ruby CSV to extract one column

Tags:

ruby

csv

I've been trying to work with getting a single column out of a csv file.

I've gone through the documentation, http://www.ruby-doc.org/stdlib/libdoc/csv/rdoc/index.html but still don't really understand how to use it.

If I use CSV.table, the response is incredibly slow compared to CSV.read. I admit the dataset I'm loading is quite large, which is exactly the reason I only want to get a single column from it.

My request is simply currently looks like this

@dataTable = CSV.table('path_to_csv.csv')

and when I debug I get a response of

#<CSV::Table mode:col_or_row row_count:2104 >

The documentation says I should be able to use by_col(), but when I try to output

<%= debug @dataTable.by_col('col_name or index') %>

It gives me "undefined method 'col' error"

Can somebody explain to me how I'm supposed to use CSV? and if there is a way to get columns faster using 'read' instead of 'table'?

I'm using Ruby 1.92, which says that it is using fasterCSV, so I don't need to use the FasterCSV gem.

like image 506
pedalpete Avatar asked May 11 '11 19:05

pedalpete


2 Answers

To pluck a column out of a csv I'd probably do something like the following:

col_data = []
CSV.foreach(FILENAME) {|row| col_data << row[COL_INDEX]}

That should be substantially faster than any operations on CSV.Table

like image 120
jkebinger Avatar answered Oct 21 '22 11:10

jkebinger


You can get the values from single column of the csv files using the following snippet.

@dataTable = CSV.table('path_to_csv.csv')
@dataTable[:columnname]
like image 14
coderhs Avatar answered Oct 21 '22 12:10

coderhs