开发者

Append row to csv file Ruby 1.9 CSV lib

开发者 https://www.devze.com 2023-01-11 20:03 出处:网络
Using Ruby 1.9开发者_StackOverflow 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 doc

Using Ruby 1.9开发者_StackOverflow 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


I think you can change the open to use ab:

CSV.open("t.csv", "ab") do |csv|


I will usually use the following to write to a csv file (Or any file)

File.open("filename", 'a+') {|f| f.write("datatowrite\n)}


File.open('filename', 'a'){ |outfile|
  CSV::Writer.generate(outfile) do |csv|
    csv << ['c1', nil, '', '"', "\r\n", 'c2']
  end
}
0

精彩评论

暂无评论...
验证码 换一张
取 消