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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With