Created
June 29, 2012 23:32
-
-
Save ryanjm/3021405 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# opens file | |
f = File.new('small.csv','r') | |
# grab all the lines (array) | |
lines = f.readlines | |
# header will be first line, take it out and strip white space | |
headers = lines.shift.strip | |
keys = headers.split(',') | |
# value to hold presidents | |
presidents = [] | |
# loop over remaining lines | |
lines.each do |line| | |
values = line.strip.split(',') | |
params = {} | |
keys.each_with_index do |key,i| | |
params[key] = values[i] | |
end | |
presidents << params | |
end | |
puts "Presidents:" | |
puts presidents |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Presidency | President | |
---|---|---|
1 | George Washington | |
2 | John Adams |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment