Skip to content

Instantly share code, notes, and snippets.

@bahelms
Last active May 9, 2022 19:46
Show Gist options
  • Save bahelms/9fe4d5f2a3bb6706f864 to your computer and use it in GitHub Desktop.
Save bahelms/9fe4d5f2a3bb6706f864 to your computer and use it in GitHub Desktop.
Merge multiple csv files into one in Ruby
require "csv"
def csv_headers
["Your", "Headers", "Here"]
end
files = Dir["file_names_*.csv"].sort_by { |f| "if you want to sort the files" }
file_contents = files.map { |f| CSV.read(f) }
csv_string = CSV.generate do |csv|
csv << csv_headers
file_contents.each do |file|
file.shift # remove the headers of each file
file.each do |row|
csv << row
end
end
end
File.open("one_csv_to_rule_them_all.csv", "w") { |f| f << csv_string }
@tanema
Copy link

tanema commented Aug 22, 2018

Just thought, since I used this as a starting point and then re-wrote it I would post what I did.

require "csv"
# create new csv for the final output with the final header:w
CSV.open("final.csv", "wb", write_headers: true, headers: ["your", "headers"]) do |csv|
  Dir["data_files/*.csv"].each do |path|  # for each of your csv files
    CSV.foreach(path, headers: true, return_headers: false) do |row| # don't output the headers in the rows
      csv << row # append to the final file
    end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment