In Ruby, I have an array and I'd like to export it into a csv file. However, I want each value of the array to be on a new row, so that the final csv file is one column containing all the array values. What's the most elegant way to do that?
Lots of ways to do this, but here's one:
require 'csv'
myarray = ["something", "something, also", "\"something\""]
CSV.open("my file.csv", "w") do |f|
myarray.each do |x|
f << [x]
end
end
<< to write each row, where here a row is just a one-element array with one item from your original arrayIf 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