Skip to content

Instantly share code, notes, and snippets.

@ugdark
Last active May 31, 2017 05:59
Show Gist options
  • Save ugdark/4b5e439b3be46632171d3fcd456cceb4 to your computer and use it in GitHub Desktop.
Save ugdark/4b5e439b3be46632171d3fcd456cceb4 to your computer and use it in GitHub Desktop.
ruby csvファイル加工サンプル
CSV.open("/Users/a_nishiyama/Kokodayo.com/ruby_examples/data/sample_new.csv", 'w') do |write|
CSV.foreach("/Users/a_nishiyama/Kokodayo.com/ruby_examples/data/sample.csv") do |columns|
# columns.each {|header, field| field += "test";p field }
# columns.each { |head, field| columns[head] = "test" }
columns.unshift("hoge") # 行の先頭に追加
columns[1] = "置換" # 行の置き換え
columns.push $. == 1 ? 'head' : 'hoge' # 行の先頭に追加
write << columns
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment