read(path, options = Hash.new) -> [Array] | CSV::Table
[permalink][rdoc]readlines(path, options = Hash.new) -> [Array] | CSV::Table
CSV ファイルを配列の配列にするために使います。 headers オプションに偽でない値を指定した場合は CSV::Table オブジェクトを返します。
require "csv"
require "pp"
File.write("test.csv", <<CSV)
id,first name,last name,age
1,taro,tanaka,20
2,jiro,suzuki,18
3,ami,sato,19
4,yumi,adachi,21
CSV
pp CSV.read("test.csv")
# => [["id", "first name", "last name", "age"],
# ["1", "taro", "tanaka", "20"],
# ["2", "jiro", "suzuki", "18"],
# ["3", "ami", "sato", "19"],
# ["4", "yumi", "adachi", "21"]]
require "csv"
File.write("test.csv", <<CSV)
id,first name,last name,age
1,taro,tanaka,20
2,jiro,suzuki,18
3,ami,sato,19
4,yumi,adachi,21
CSV
table = CSV.read("test.csv", headers: true)
p table.class # => CSV::Table
p table[0] # => #<CSV::Row "id":"1" "first name":"taro" "last name":"tanaka" "age":"20">