Reputation: 453
I have a CSV file and I want to merge these records into an existing CSV file. However, the two files have headers that are named differently. How can I cleanly and efficiently rename the CSV headers so that the match the file I'm merging to?
Upvotes: 4
Views: 1648
Reputation: 453
Answer:
CSV::HeaderConverters[:map_to_main] = lambda do |header|
# work your magic here
header
end
CSV.open(file,
headers: true,
header_converters: :map_to_main).to_a.map(&:to_hash)
Upvotes: 8