Skip to content

Instantly share code, notes, and snippets.

@jshawl jshawl/app.rb
Last active Apr 12, 2018

Embed
What would you like to do?
Combine Two CSVs side-by-side
require 'csv'
# note: << in ruby is the same as .push
# [1,2] << 15 #=> [1,2,15]
new_csv = []
CSV.foreach('csv1.csv') do |csv1|
new_csv << csv1
end
line = 0
CSV.foreach('csv2.csv') do |csv2|
new_csv[line].push(*csv2) # check out python extend method
line = line + 1
end
CSV.open('csv_combined.csv','w') do |csv|
csv << new_csv.shift # put the headers in the array
new_csv.each do |row|
csv << row
end
end
one two
a b
three four
c d
one two three four
a b c d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.