csv 文件写入
require 'csv'
title = ["col1", "col2"]
contents = [["row11", "row12"], ["row21", "row22"]]
csv1 = CSV.open("test1.csv", "wb") do |csv|
# write file title
csv << title
# write file body
contents.each do |line|
csv << line
end
end
puts csv1.class
puts csv1.inspect
# UTF-8编码的文件,开头会加入BOM来表明编码方式
csv2 = CSV.generate("\xEF\xBB\xBF") do |csv|
# write file title
csv << title
# write file body
contents.each do |line|
csv << line
end
end
File.open("test2.csv", "wb"){|f| f << csv2}
puts csv2.class
puts csv2.inspect
Array
[["row11", "row12"], ["row21", "row22"]]
String
"col1,col2\nrow11,row12\nrow21,row22\n"
csv 文件读取
test.csv文章来源地址https://www.toymoban.com/news/detail-723257.html
col1,col2
row11,row12
row21,row22
按行读取
require 'csv'
csv3 = []
CSV.open("test1.csv") do |csv|
csv.each do |line|
csv3 << line
end
end
puts csv3.class
puts csv3.inspect
csv4 = []
File.open("test2.csv", "rb") do |f|
f.each do |line|
csv4 << line
end
end
puts csv4.class
puts csv4.inspect
Array
[["col1", "col2"], ["row11", "row12"], ["row21", "row22"]]
Array
["\xEF\xBB\xBFcol1,col2\n", "row11,row12\n", "row21,row22\n"]
以键值对形式读取
require 'csv'
rows = CSV.read("test.csv", headers:true)
title = rows.headers
contents = []
rows.each do |row|
contents << row.to_h
end
puts title.inspect
puts contents.inspect
puts
rows = CSV.parse("col1,col2\nrow11,row12\nrow21,row22", headers:true)
title = rows.headers
contents = []
rows.each do |row|
contents << row.to_h
end
puts title.inspect
puts contents.inspect
["col1", "col2"]
[{"col1"=>"row11", "col2"=>"row12"}, {"col1"=>"row21", "col2"=>"row22"}]
["col1", "col2"]
[{"col1"=>"row11", "col2"=>"row12"}, {"col1"=>"row21", "col2"=>"row22"}]
文章来源:https://www.toymoban.com/news/detail-723257.html
到了这里,关于Ruby 之 csv 文件读写的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!