如何对csv行进行求和

时间:2017-11-07 15:26:03

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

我有以下代码从txt文件中检索行,我想总结item_price的所有值,以便能够在我的应用程序中使用它。我希望从块中检索出这个值:

CSV.foreach(dir, col_sep: "\t", headers: true).map do |row|

按照代码:

module ParseText
  def self.parse
    require 'csv'
    dir = Dir.glob("./public/system/file_attaches/files/*/*/*/*/*").max_by {|f| File.mtime(f)}
    CSV.foreach(dir, col_sep: "\t", headers: true).map do |row|
      records = row.to_h
      purchaser = records["purchaser name"]
      item_description = records["item description"]
      item_price = records["item price"]
      purchase_count = records["purchase count"]
      merchant_address = records["merchant_address"]
      merchant_name = records["merchant name"]

      Merchant.create(name: merchant_name)
      MerchantAddress.create(street: merchant_address, merchant_id: Merchant.last.id)
      Purchaser.create(name: purchaser, count: purchase_count, merchant_id: Merchant.last.id)
      Item.create(description: item_description, price: item_price, merchant_id: Merchant.last.id, purchaser_id: Purchaser.last.id )
    end
  end
end

1 个答案:

答案 0 :(得分:2)

    .app-body {
        overflow: auto;
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        top: 64px;
    }

    .app-container {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
    }