在Ruby中将数组输出为CSV [英] Output array to CSV in Ruby
本文介绍了在Ruby中将数组输出为CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用Ruby读取CSV文件到数组很容易,但我找不到任何有关如何将数组写入CSV文件的好文档。任何人都可以告诉我该怎么做?
It's easy enough to read a CSV file into an array with Ruby but I can't find any good documentation on how to write an array into a CSV file. Can anyone tell me how to do this?
我使用Ruby 1.9.2,如果这很重要。
I'm using Ruby 1.9.2 if that matters.
推荐答案
到文件:
require 'csv'
CSV.open("myfile.csv", "w") do |csv|
csv << ["row", "of", "CSV", "data"]
csv << ["another", "row"]
# ...
end
到字符串:
require 'csv'
csv_string = CSV.generate do |csv|
csv << ["row", "of", "CSV", "data"]
csv << ["another", "row"]
# ...
end
以下是有关CSV的当前文档: http://ruby-doc.org /stdlib/libdoc/csv/rdoc/index.html
Here's the current documentation on CSV: http://ruby-doc.org/stdlib/libdoc/csv/rdoc/index.html
这篇关于在Ruby中将数组输出为CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文