user28
user28

Reputation: 259

How to read specific columns of a zipped CSV file

I used the code below to read the contents of a zipped CSV file.

Zip::ZipFile.foreach(file) do |entry|
  istream = entry.get_input_stream
  data = istream.read
  #...
end

It gives me the entire content of the text (CSV) file with headers like below:

NAME AGE GENDER NAME1 29 MALE NAME2 30 FEMALE

but I need specific data of the column. For example, I want to display only the names (NAME). Please help me proceed with this.

Upvotes: 2

Views: 1158

Answers (2)

fearless_fool
fearless_fool

Reputation: 35239

Though your example shows ZipFile, you're really asking a CSV question. First, you should check the docs in http://www.ruby-doc.org/stdlib-2.0/libdoc/csv/rdoc/CSV.html

You'll find that if you parse your data with the :headers => true option, you'll get a CSV::table object that knows how to extract a column of data as follows. (For obvious reasons, I wouldn't code it this way -- this is for example only.)

require 'zip'
require 'csv'

csv_table = nil
Zip::ZipFile.foreach("x.csv.zip") do |entry|
  istream = entry.get_input_stream
  data = istream.read
  csv_table = CSV.parse(data, :col_sep => " ", :headers => true)
end

With the data you gave, we need `col_sep => " " since you're using spaces as column separators. But now we can do:

>> csv_table["NAME"]   # extract the NAME column
=> ["NAME1", "NAME2"]

Upvotes: 5

gmaliar
gmaliar

Reputation: 5489

First you can use this for reference:

http://www.ruby-doc.org/stdlib-2.0/libdoc/csv/rdoc/CSV.html

If you have a string you can do

array = CSV.parse("data")

This would give you an array of arrays, one for each line. Now if you know that the first column for each line is the name you can just manipulate that array i.e

array.map { |line| line[0] }.join(",") # returns NAME,<name>,<name>,<name> ...

Upvotes: 0

Related Questions