Read csv file. Beginner's HELP!

Hai,

Im new to Ruby programming. I got a job to create a program that can
obtain the content from a .csv file into .yml file. However at this
stage, im having trouble reading the .csv file. Here is the code i use:

#!/usr/bin/ruby

require ‘rubygems’
require ‘csv’

CSV.open(‘C:/Users/nizam/test_q.csv’, ‘r’) do |row|
p row
end

puts “Hello World”

Here is the error message i get:
<#CSV io_type:File io_path:“C:/Users/nizam/test_q.csv” encoding:UTF-8
lineno:0 col_sep:"," row_sep:"\n" quote_char:""">

Btw, im using Netbeans IDE 6.9.1 to write my script. I know this is just
a silly error but still cant figure it out. Hope some one can help.
Thanks

Nizam

On Jan 11, 2011, at 4:07 PM, Kamarulnizam R. wrote:

Hai,

Hello.

Im new to Ruby programming.

Welcome.

puts “Hello World”

Here is the error message i get:
<#CSV io_type:File io_path:“C:/Users/nizam/test_q.csv” encoding:UTF-8
lineno:0 col_sep:"," row_sep:"\n" quote_char:""">

That’s not actually an error message. It’s an inspection of the CSV
object open() passed into your block. It looks like you were expecting
a row of data though, so switch open() to foreach() and that should get
you going.

James Edward G. II

Thanks James,

The error gone. But new error came out:

C:/Ruby192/lib/ruby/1.9.1/csv.rb:1198:in delete': can't convert Symbol into String (TypeError) from C:/Ruby192/lib/ruby/1.9.1/csv.rb:1198:inforeach’
from C:/Ruby192/new_main.rb:9:in `’

I think it is because of the symbol in my csv file that cant be
converted. How to solve this so that i can read the whole document?
Please find the attached csv file.

One more thing, do think it is better for me to use the standard csv
library or faster csv library? As for my purpose to obtain data from csv
file and convert it into YAML file.

Thank you so much for a very quick reply. Appreciate that

Nizam

On Jan 11, 2011, at 4:27 PM, Kamarulnizam R. wrote:

The error gone. But new error came out:

   from C:/Ruby192/new_main.rb:9:in `<main>'

I need to see line 9 from new_main.rb.

I think it is because of the symbol in my csv file that cant be
converted.

Na, this error is talking about Ruby, not the CSV content.

One more thing, do think it is better for me to use the standard csv
library or faster csv library?

It looked like you are on Ruby 1.9. In that version, the CSV code is
the code from FasterCSV. You are already using the good stuff.

James Edward G. II

Hi,

This is the code from line 9:

CSV.foreach(‘C:/Users/nizam/test_q.csv’, ‘r’) do |row|

What do you think is the problem? Thank you

Nizam

Hi James,

Yeah It works. Thank you so much

Nizam

On Jan 11, 2011, at 4:50 PM, Kamarulnizam R. wrote:

This is the code from line 9:

CSV.foreach(‘C:/Users/nizam/test_q.csv’, ‘r’) do |row|

What do you think is the problem?

It’s my fault. I forgot to tell you to remove the read mode. It’s
implied by foreach(). You want:

CSV.foreach(‘C:/Users/nizam/test_q.csv’) do |row|

James Edward G. II