Learn Why to Code

A brief introduction to practical programming

« Previous: Switching data locations Next: Plucking out values »

Reading From a Spreadsheet-like File

The problem: How to read data from a spreadsheet-like file, such as CSV?
The solution: Use the FasterCSV library and Hash objects to simplify reading the datafile.

Lesson content is currently in draft form.

The CSV (or FasterCSV) library

(draft)

If you have Ruby 1.8, install the FasterCSV gem.

If you have Ruby 1.9, FasterCSV is part of the CSV library, so you just need to have the require statement.

In our my_code.rb file, I’ve handled both conditions like so:

1
2
3
4
5
if RUBY_VERSION > "1.9"
  require 'csv'
else
  require 'fastercsv'
end

CSV Files

(draft)

The Sunlight Foundation maintains a spreadsheet of Congressmember data, including social media usernames.

Download our mirroed copy for this tutorial here: http://nottwitter.danwin.com/sunlight-foundation-congress-master.csv

It is also in your working-directory

Parsing CSV

We could parse with the string split method, but even a simple CSV file can have many ugly variations and irregularities.

Use the CSV library instead:

1
2
3
4
5
6
7
data = get_data_file(CSV_DATA_MAIN_FILENAME)

CSV.new(data)

# or

FasterCSV.new(data)

The headers option

1
FasterCSV.new(data, {:headers=>true})

Each data row is represented as a hash, with the column names conveniently used as keys.

Convenience method for congressmembers

In our my_code.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
def get_congress_data_from_file(fname)
  # pre: fname is a CSV file where congressmember data is located
  # returns: an array of Hash objects

  data = get_data_file(fname)


  if RUBY_VERSION > "1.9"
    return CSV.new(data, {:headers=>true} )
  else
    return FasterCSV.new(data, {:headers=>true})
  end
end

Comments