Last active
August 29, 2015 14:07
-
-
Save ta1kt0me/ecf1ede307d6a93a90a7 to your computer and use it in GitHub Desktop.
CSVのデータ行の合計を出力する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ###```csv test.csv | |
| # j,m,h,s | |
| # 1,2,3,4 | |
| # 5,6,7,8 | |
| # 9,0,1,2 | |
| ###``` | |
| data = CSV.open("test.csv","r") | |
| header = data.take(1)[0] | |
| calc = Array.new(header.size).map{[]} | |
| data.each {|v| v.each_with_index {|v, i| calc[i] << v }} | |
| body=[] | |
| calc.each {|col| body << col.map{|v| v.to_i}.inject(:+);} | |
| puts "#{header.join(',')}¥n#{body.join(',')}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment