0722デフォルトの名無しさん (ワッチョイ 5f2c-+BGV)
2019/09/30(月) 20:12:39.70ID:auJmx+hY02列以外のすべての列を削除したりできますか?
ただし、列名・列数は、ファイルごとに変化します
require 'csv'
field_name_1 = "AAA"
field_name_2 = "BBB"
header = CSV::Row.new( [ field_name_1, field_name_2 ], [], header_row: true ) # ヘッダー行
table = CSV::Table.new( [ header ] ) # ヘッダー行を追加する
# 引数は、入力ファイル名。ヘッダー行あり
CSV.foreach( ARGV[ 0 ], headers: true ) do |row| # 1行ずつ処理する
ary = [ row[ field_name_1 ], row[ field_name_2 ] ]
table << CSV::Row.new( [ field_name_1, field_name_2 ], ary ) # 1行ずつ追加する
end
puts table