加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Ruby:如何使用dump方法将数据输出到csv文件?

发布时间:2020-12-17 02:36:13 所属栏目:百科 来源:网络整理
导读:我尝试使用 ruby标准csv lib将对象的arr转储到名为’a.csv’的csv.file中 http://ruby-doc.org/stdlib-1.9.3/libdoc/csv/rdoc/CSV.html#method-c-dump dump(ary_of_objs,io = "",options = Hash.new) 但在这种方法中,我如何转储到文件中? 没有这样的例子存
我尝试使用 ruby标准csv lib将对象的arr转储到名为’a.csv’的csv.file中

http://ruby-doc.org/stdlib-1.9.3/libdoc/csv/rdoc/CSV.html#method-c-dump

dump(ary_of_objs,io = "",options = Hash.new)

但在这种方法中,我如何转储到文件中?
没有这样的例子存在和帮助.我谷歌没有为我做的例子……

此外,文档说…

The next method you can provide is an instance method called
csv_headers(). This method is expected to return the second line of
the document (again as an Array),which is to be used to give each
column a header. By default,::load will set an instance variable if
the field header starts with an @ character or call send() passing the
header as the method name and the field value as an argument. This
method is only called on the first object of the Array.

任何人都知道如何将实例方法csv_headers()传递给此转储函数?

解决方法

我还没有测试过,但看起来应该将io设置为文件.根据您链接的文档“io参数可用于序列化为文件”

就像是:f = File.open(“filename”)dump(ary_of_objs,io = f,options = Hash.new)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读