Read csv file. Beginner's HELP!!

K

Kamarulnizam Rahim

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
 
J

James Edward Gray II

Hai,
Hello.

Im new to Ruby programming.
Welcome.

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:
=20
#!/usr/bin/ruby
=20
require 'rubygems'
require 'csv'
=20
CSV.open('C:/Users/nizam/test_q.csv', 'r') do |row|
p row
end
=20
puts "Hello World"
=20
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 Gray II=
 
K

Kamarulnizam Rahim

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:in `foreach'
from C:/Ruby192/new_main.rb:9:in `<main>'

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

Attachments:
http://www.ruby-forum.com/attachment/5715/test_q.csv
 
J

James Edward Gray II

The error gone. But new error came out:
=20
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=20
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=20=
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 Gray II
 
K

Kamarulnizam Rahim

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
 
J

James Edward Gray II

This is the code from line 9:
=20
CSV.foreach('C:/Users/nizam/test_q.csv', 'r') do |row|
=20
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|=20

James Edward Gray II=
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,808
Messages
2,569,686
Members
45,453
Latest member
RamonMcCom

Latest Threads

Top