Rectangle 27 1

How do I create a new CSV file in Ruby?


CSV.foreach("file.csv") do |row|
  puts row #first row would be ["animal", "count", "price"] - etc.
end
CSV.open("file.csv", "a+") do |csv|
  csv << ["cow", "3","2500"]
end
animal,count,price
fox,1,$90.00
cow,3,2500
require "csv"
CSV.open("file.csv", "wb") do |csv|
  csv << ["animal", "count", "price"]
  csv << ["fox", "1", "$90.00"]
end

Almost the same forumla as above only instead of using "wb" mode, we'll use "a+" mode. For more information on these see this stack overflow answer: What are the Ruby File.open modes and options?

As mikeb pointed out, there are the docs - http://ruby-doc.org/stdlib-1.9.3/libdoc/csv/rdoc/CSV.html - Or you can follow along with the examples below (all are tested and working):

In this file we'll have two rows, a header row and data row, very simple CSV:

Now when we open our file.csv we have:

Now you know how to copy and to write to a file, to read a CSV and therefore grab the data for manipulation you just do:

Of course this is like one of like a hundred different ways you can pull info from a CSV using this gem. For more info I suggest visiting the docs now that you have a primer: http://ruby-doc.org/stdlib-1.9.3/libdoc/csv/rdoc/CSV.html

What if I want to open without writing to right away? Just don't use the block?

result, a file called "file.csv" with the following:

Note
Rectangle 27 0

How do I create a new CSV file in Ruby?


CSV.foreach("file.csv") do |row|
  puts row #first row would be ["animal", "count", "price"] - etc.
end
CSV.open("file.csv", "a+") do |csv|
  csv << ["cow", "3","2500"]
end
animal,count,price
fox,1,$90.00
cow,3,2500
require "csv"
CSV.open("file.csv", "wb") do |csv|
  csv << ["animal", "count", "price"]
  csv << ["fox", "1", "$90.00"]
end

Almost the same forumla as above only instead of using "wb" mode, we'll use "a+" mode. For more information on these see this stack overflow answer: What are the Ruby File.open modes and options?

As mikeb pointed out, there are the docs - http://ruby-doc.org/stdlib-1.9.3/libdoc/csv/rdoc/CSV.html - Or you can follow along with the examples below (all are tested and working):

In this file we'll have two rows, a header row and data row, very simple CSV:

Now when we open our file.csv we have:

Now you know how to copy and to write to a file, to read a CSV and therefore grab the data for manipulation you just do:

Of course this is like one of like a hundred different ways you can pull info from a CSV using this gem. For more info I suggest visiting the docs now that you have a primer: http://ruby-doc.org/stdlib-1.9.3/libdoc/csv/rdoc/CSV.html

What if I want to open without writing to right away? Just don't use the block?

result, a file called "file.csv" with the following:

Note
Rectangle 27 0

How do I create a new CSV file in Ruby?


thanks for the link. I would be referring it. Can I edit the csv file through ruby? I mean can I update a column values in csv ? later get hash of only required columns?

Note
Rectangle 27 0

How do I create a new CSV file in Ruby?


thanks for the link. I would be referring it. Can I edit the csv file through ruby? I mean can I update a column values in csv ? later get hash of only required columns?

Note