4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

覚えた使えるRubyトリック _ CSVファイルのRead &Write

Last updated at Posted at 2016-03-15

2016年 初春。
アルコール漬けのチンピラからプログラマに転身した私が覚えたトリックを備忘録兼ねてまとめるTips_Ruby編


RubyでCSVファイルの読み書きをする機会があって、なかなか忘れそうなので書いとく。

csvファイルから1行ずつ書いてあるKEY文字列を読み込んで適当な配列に格納。
CSV.foreach("Read_hoge.csv") do |line|
  key = line.chomp # chompで末尾の改行文字を除去
  keys = []
  keys << key
end

csvファイルに書き込む
write = [id,name]
CSV.open("Write_hoge.csv","w") do |csv|
  csv << write
end

書き込みのときの、"w"を忘れててハマった。ライトちゃん。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?