Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append row to csv file Ruby 1.9 CSV lib

Tags:

ruby

csv

ruby-1.9

Using Ruby 1.9 and CSV lib, I can't seem to append a row. The example in the documentation opens the file, and overwrites the row. What is the correct way to append rows to the document?

Example from documentation:

require 'csv' CSV.open("path/to/file.csv", "wb") do |csv|   csv << ["row", "of", "CSV", "data"]   csv << ["another", "row"]   # ... end 
like image 698
cheolho minale Avatar asked Aug 17 '10 22:08

cheolho minale


People also ask

How do I append a row to a csv file?

If you need to append row(s) to a CSV file, replace the write mode ( w ) with append mode ( a ) and skip writing the column names as a row ( writer.

How do I append an existing CSV file in R?

In order to append the data to a CSV File, use the write. table() method instead and set the parameter, append = TRUE.


1 Answers

I think you can change the open to use ab:

CSV.open("t.csv", "ab") do |csv| 
like image 116
Mark Wilkins Avatar answered Oct 15 '22 02:10

Mark Wilkins