在 RUBY CSV 中 - 如何对 1 列排序的 csv 数据求和并确保保持对数据的约束或限制



我正在开发一个 csv 阅读器,并希望对数据进行操作,我想对一列求和 - 年,但请确保不要超过一定数量的点 - 比如 100 万。我无法对列求和,它正在递增。我的约束是只有 10 + 年

csv 看起来像这样

id, numbers, years,
1, 50000, 10
2, 40000, 12  
3, 50000, 9

我的红宝石文件看起来像这样

#!/usr/bin/env ruby
require 'pry'
require 'rubygems'
require 'fastercsv'
mycsv = CSV.read('input.csv', headers:true, col_sep: ', ', header_converters: :symbol, converters: :integer) # read the entire file into a 'loans' variable

new_mycsv = mycsv.find_all do |p|
 p[:years].between?(10, 12)
end
puts new_mycsv.inspect
#Sum csv data on first row
new_mycsv.first << "SUM"

new_mycsv[1..-1].each{|row| row << row.inject(:+)}
CSV.open("output.csv","w") do |out|
    new_mycsv.sort_by { |row| each do |row| }
   p out << row
 end
end

按照发布的方式运行代码:

$ ruby -w t_op.rb 
t_op.rb:23: warning: shadowing outer local variable - row
t_op.rb:23: syntax error, unexpected '}'
    new_mycsv.sort_by { |row| each do |row| }
                                             ^
t_op.rb:26: syntax error, unexpected keyword_end, expecting '}'

对代码的一些注释:

  • 发布无错误的代码,以便我们可以复制粘贴并对其进行处理
  • 仅发布回答问题所需的代码。例如

    require 'pry'
    

    未使用,不需要解决发布的问题

  • 使用 -w 选项运行 Ruby 并更正所有警告
  • 此声明 :

    new_mycsv.first << "SUM"
    

    "SUM"连接到new_mycsv的第一个元素,导致数组更改为:

    1,50000,10,SUM
    2,40000,12
    
  • 此声明 :

    new_mycsv[1..-1].each{|row| row << row.inject(:+)}
    

    忽略第一行并将某些内容连接到其他行,从而导致数组更改为:

    1,50000,10
    2,40000,12,"[:id, 2, :numbers, 40000, :years, 12]"
    

这是一个解决方案(我不确定我是否很好地理解not go past a certain number of points的含义):

require 'csv'
mycsv = CSV.read('input.csv', headers:true, col_sep: ', ', header_converters: :symbol, converters: :integer) # read the entire file into a 'loans' variable
new_mycsv = mycsv.find_all { | row | row[:years].between?(10, 12) }
puts '---- new_mycsv'
puts new_mycsv
puts '---- selecting rows until the limit of numbers is reached'
numbers_limit  = 100_000
sum_of_numbers = 0
mycsv_limited  = []
new_mycsv.each do | row |
    number = row[:numbers]
    puts "row[:numbers]=#{number}  sum_of_numbers=#{sum_of_numbers}"
    break if sum_of_numbers + number >= numbers_limit
    sum_of_numbers += number
    puts "sum_of_numbers=#{sum_of_numbers}"
    mycsv_limited << row
end
CSV.open("output.csv","w") do |out|
    mycsv_limited.sort_by{ | row | row[:numbers] }.each { | row | out.puts row }
end

文件输入.csv :

id, numbers, years,
1, 50000, 10
2, 40000, 12  
3, 50000, 9
4, 50000, 11
5, 40000, 11  

执行:

$ ruby -w t.rb 
---- new_mycsv
1,50000,10
2,40000,12
4,50000,11
5,40000,11
---- selecting rows until the limit of numbers is reached
row[:numbers]=50000  sum_of_numbers=0
sum_of_numbers=50000
row[:numbers]=40000  sum_of_numbers=50000
sum_of_numbers=90000
row[:numbers]=50000  sum_of_numbers=90000

文件输出.csv :

2,40000,12
1,50000,10

相关内容

最新更新