How to read CSV in Ruby 1.9.2

I am totally new to rails. I have a school project that requires me to
import data from a CSV file to mysql database. I read the documentation
regarding CSV in ruby 1.9 but I don't understand since I am very new to
to this. Can someone show me a very simple example how to read data from
CSV file using the ruby 1.9 CSV. I know this might be too much to ask
but I really need help. Pls be patient with me... Pls..

Tom

···

--
Posted via http://www.ruby-forum.com/.

Simple example...

require "csv" # require the CSV library
CSV.foreach("path/to/file.csv") do |row| # open your file and loop through
the rows
  p row # print each row's contents
  p row.count # count the number of values in
each row (CSV::Row is Enumerable)
  ...
end

···

On Wed, Feb 16, 2011 at 6:55 AM, Tom Llobrera <phantom.hitman.7@gmail.com>wrote:

I am totally new to rails. I have a school project that requires me to
import data from a CSV file to mysql database. I read the documentation
regarding CSV in ruby 1.9 but I don't understand since I am very new to
to this. Can someone show me a very simple example how to read data from
CSV file using the ruby 1.9 CSV. I know this might be too much to ask
but I really need help. Pls be patient with me... Pls..

Tom

--
Posted via http://www.ruby-forum.com/\.

I wondered that myself last week and came up with..

@a =
f = File.open("#{filename}", "r")
             f.readlines.each {|e|
                 e.chomp!
                 b=e.split(";") #or comma's if you prefer.
                @a.push(b)
                    }
             f.close

This gives you an array of arrays.

Eelco
(I gues i should search the api more.. Didn't know about the CVS class/module)..

Op 16-2-2011 6:55, Tom Llobrera schreef:

···

I am totally new to rails. I have a school project that requires me to
import data from a CSV file to mysql database. I read the documentation
regarding CSV in ruby 1.9 but I don't understand since I am very new to
to this. Can someone show me a very simple example how to read data from
CSV file using the ruby 1.9 CSV. I know this might be too much to ask
but I really need help. Pls be patient with me... Pls..

Tom

Taking that one step further, an import using Rails 3 usually goes something like this:

  require "csv"
  CSV.new( params[:file].tempfile,
           :headers => true,
           :header_converters => :symbol ).each do |row|
    MyModel.create!(row.to_hash)
  end

Hope that helps.

James Edward Gray II

···

On Feb 16, 2011, at 1:43 AM, Chris Kottom wrote:

Simple example...

require "csv" # require the CSV library
CSV.foreach("path/to/file.csv") do |row| # open your file and loop through
the rows
p row # print each row's contents
p row.count # count the number of values in
each row (CSV::Row is Enumerable)
...
end

I wondered that myself last week and came up with..

this code is not really a good solution

@a =
f = File.open("#{filename}", "r")

this could be much simpler written as File.open(filename,"r")

           f.readlines.each {|e|
               e.chomp!
               b=e.split(";") #or comma's if you prefer.

with this line you get rather false results if you have a string value in a
csv file which contains ',' oder ';'.

···

On 17 February 2011 10:35, Catsquotl <catsquotl@gmail.com> wrote:

--
Thomas Preymesser
thopre@gmail.com