解析唯一记录CSV

时间:2014-06-18 05:29:14

标签: ruby-on-rails ruby arrays csv hash

我的CSV包含:

"ScanDateTime",       "ScanDate",   "PrintIDName",     "Section","Page","ZoneNumber","Cyan","Magenta","Yellow","Black"
"2013-12-11 12:19:57","2013/12/11","Press 40: Wed- First Good","D","1","1","0.543378","0.52948","0.552927","0.558979"
"2013-12-11 12:19:57","2013/12/11","Press 40: Wed- First Good","D","1","2","0.53521","0.517317","0.543839","0.549084"
"2013-12-11 12:19:57","2013/12/11","Press 40: Wed- First Good","D","1","3","0.527715","0.509898","0.526554","0.540249"
"2013-12-11 12:19:57","2013/12/11","Press 41: Wed- First Good","D","1","4","0.540551","0.517619","0.535687","0.551253"
"2013-12-12 12:19:57","2013/12/13","Press 40: Wed- First Good","D","1","5","0.552748","0.527429","0.546477","0.563483"
"2013-12-12 12:19:57","2013/12/13","Press 40: Wed- First Good","D","1","6","0.547891","0.526122","0.534965","0.559098"
"2013-12-12 12:19:57","2013/12/13","Press 40: Wed- First Good","D","1","7","0.544341","0.53547","0.552707","0.563052"
"2013-12-12 12:19:57","2013/12/13","Press 41: Wed- First Good","D","1","8","0.535602","0.523324","0.546354","0.552857"
"2013-12-12 12:23:36","2013/12/13","Press 41: Wed- First Good","D","10","1","0.534327","0.522695","0.546672","0.550833"

在1到8之后看到zone_number,它会重复从1区到8区的迭代。在8区后,页码会改变。

这就是我想要的:

我想从ScanDateTime及其相应的ScanDates和ScanDates'中选择唯一的日期。相应的PrintIDName和PrintIDNames'相应的Section,Pages,ZoneNumber及其颜色值。

我可能处于哈希结构或散列数组或其他任何东西但是想要解析它。

我正在谷歌上搜索并将其堆叠两天但没有成功。

我到目前为止尝试的是复制数组中的所有行,然后尝试从数组中读取但是再次没有成功。

更新

这就像一对多的关系。 1 ScanDateTime has_many ScanDate。

1 ScanDate has_many PrintIDName

1 PrintIDName has_many Sections, Pages

1 Section+Page has_many ZoneNumbershas_many colors through ZoneNumber

我想要的是附加结构。

What i Want is

注意:在图像中,我只提到了一个清晰度流

我尝试过的是:

CSV_CONFIG = {headers: true, header_converters: :symbol, converters: :numeric}

CSV.foreach(CSV_FILE, CSV_CONFIG) do |row|
    page_densities << ::DensityFeed.new(row: row).row_densities
end

class DensityFeed < Struct.new(:scandatetime, :scan_date, :printidname, :section, :page, :zone_number, :cyan, :magenta, :yellow, :black, :page_master_id, :page_number, :issue_date)
  COLOR_IDS = {cyan: 1, magenta: 2, yellow: 3, black: 4}

  def initialize(attr)
    @row = attr[:row]
  end

  def row_densities()
    psm_id = page_master_id
    ph_id = page_header_id
    pn = page_number
    col_indx = @row[:zonenumber]
    COLOR_IDS.keys.map do |color|
      PageDensity.create(row_index: COLOR_IDS[color], column_index: col_indx, color_id: COLOR_IDS[color], density: @row[color], page_score_master_id: psm_id, page_number: pn, page_header_id: ph_id)
    end
  end    
end

0 个答案:

没有答案