I am trying to figure out the best way to pull a value from a CSV file called lookup.csv
based on a value in master.csv
, and then save the new file as
I'm using 1.9, where FasterCSV
is available as CSV
in the standard lib. First I'd create a lookup hash out of lookup.csv
:
cities = Hash[CSV.read('lookup.csv', :col_sep => ' | ').to_a[1..-1]]
If the file is very big, you might want to iterate over it with CSV.foreach
and build the hash row by row:
cities = {}
CSV.foreach('lookup.csv', :col_sep => ' | ', :headers => true, :return_headers => false) do |line|
cities[line['City']] = line['City ID']
end
Then iterate over master.csv
, do a lookup of the city in the hash and write that to output.csv
:
CSV.open('output.csv', "w", :headers => ['First Name', 'Last Name', 'City ID'], :write_headers => true) do |output|
CSV.foreach('master.csv', :col_sep => ' | ', :headers => true, :return_headers => false) do |line|
output << [line['First Name'], line['Last Name'], cities[line['City']]]
end
end