I'm attempting to append a new column to each row of an existing csv file. The following code executes what I want:
csv_info = CSV.read("foo.csv")
csv_info.each do |info|
info << new_object
end
giving me the following in memory when I use pry
:
[[uuid0, account0, url0, new_object],
[uuid1, account1, url1, new_object]]
This doesn't write to file since it's a CSV.read
instead of a CSV.open("foo.csv","w")
. When I attempt to write over the file with the csv_info
object:
csv_info = CSV.read("foo.csv")
csv_info.each do |info|
info << new_object
end
CSV.open("foo.csv", "w") do |old_csv|
old_csv << csv_info
end
The file ends up as such:
[["[\"<uuid0>\", \"<account0>\", \"<url0>\", \"<new_object>\"]",
"[\"<uuid1>\", \"<account1>\", \"<url1>\", \"<new_object>\"]"]]
How can I append without impacting the csv formatting?