0
0

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.

csv

Posted at

書き込み

  • headerのセット
  • パスのセット

require 'csv'


peoples = [
 ['alice', 14, 'femal'],
 ['alice', 15, 'femal'],
 ['alice', 16, 'femal'],
]


headers = ["名前", "年齢", "性別"]

file_path = "./test.csv"

CSV.open(file_path, 'w', headers: headers ) do |row|
  row << headers
  peoples.each do |pe|
    row << pe
  end
end

読み込み

  • headers trueを適応
  • 要素を出力する
require 'csv'


file_path = "./test.csv"


CSV.foreach(file_path, headers: true) do |row|
  p row
end

CSV.foreach(file_path, headers: true) do |row|
    p row["名前"]
    p row["年齢"]
    p row["性別"]
end
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?