Rails 明智地编写一个 csv 文件列 [英] Rails write a csv file column wise

查看:48
本文介绍了Rails 明智地编写一个 csv 文件列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢使用 rails 的内置 csv 库将我的 rails 应用程序中的数据集导出为 csv 文件.通常一个 csv 文件是按行写入的,就像我下面的例子一样,它来自我的 datasets_controller.rb:

I like to export a dataset from my rails application as a csv file using the builtin csv library of rails. Usually a csv file is written row wise like in my example below which comes from my datasets_controller.rb:

require 'csv'

dataset = Dataset.find(6)
dataset_headers = dataset.datacolumns.collect { |dc| dc.columnheader }

csv_file = CSV.generate do  |csv|                                                                                                           
   csv << dataset_headers
end

现在我的问题是我是否也可以像这样明智地编写我的 csv 文件列?

And now my question is if I could also write my csv files column wise like this?

require 'csv'    

dataset_columns = Datacolumn.all(:conditions => ["dataset_id = ?", 6], :order => "columnnr ASC").uniq 

csv_file = CSV.generate do  |csv|                                                                                                           
   csv << "here put one after another all my data columns"
end

根据道格拉斯的建议,我想出了下面的感冒.

Based on Douglas suggestion I came up with the colde below.

 data_columns=Datacolumn.all(:conditions => ["dataset_id = ?", dataset.id], :order => "columnnr ASC").uniq

    CSV.generate do |csv|
        value=Array.new 
        data_columns.each do |dc|
          value << dc.columnheader
          dc.sheetcells.each do |sc|
            if sc.datatype && sc.datatype.is_category? && sc.category 
              value << sc.category.short
            elsif sc.datatype && sc.datatype.name.match(/^date/) && sc.accepted_value
              value << sc.accepted_value.to_date.to_s
            elsif sc.accepted_value
              value << sc.accepted_value
            else
              value << sc.import_value
            end 
          end 
          csv << value
          value = Array.new 
        end 
      end 

这种情况下的输出没有转置,看起来像这样:

The output is not transposed for this case and looks like this:

height,10,2,<1,na
fullauthor,Fortune,(Siebold & Zucc.) Kuntze,Fortune,(Siebold & Zucc.) Kuntze
Year,1850,1891,1850,1891
fullname,Mahonia bealei,Toxicodendron sylvestre,Mahonia bealei,Toxicodendron sylvestre

但是当我更改将 csv 写入的行时

But when I change the line which writes the csv to

csv << value.transpose

我收到一个错误,告诉我它无法将字符串转换为数组来执行此操作.有人知道如何解决这个问题吗?

I get an error which tells me that it could not convert a string to array to do that. Anybody an Idea how to fix this?

对此的任何帮助将不胜感激.

Any help with this would be appreciated.

最佳克拉斯

推荐答案

您可以使用 Array#transpose,它将您的行翻转为列.一个简单的例子:

You could use Array#transpose, which will flip your rows to columns. A simple example:

> a = [['name', 'charles', 'dave'],['age', 24, 36],['height', 165, 193]]
=> [["name", "charles", "dave"], ["age", 24, 36], ["height", 165, 193]]
> a.transpose
=> [["name", "age", "height"], ["charles", 24, 165], ["dave", 36, 193]]

因此,假设 dataset_columns 是一个数组:

Thus, assuming dataset_columns is an array:

require 'csv'    

dataset_columns = Datacolumn.all(:conditions => ["dataset_id = ?", 6], :order => "columnnr ASC").uniq 

csv_file = CSV.generate do  |csv|                                                                                                           
   csv << dataset_columns.transpose
end

这篇关于Rails 明智地编写一个 csv 文件列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆