# Read .csv file. Cant obtain data

**URL:** https://rubytalk.org/t/read-csv-file-cant-obtain-data/61424
**Category:** ruby-talk
**Created:** [12 January 2011 01:12 UTC](https://rubytalk.org/t/read-csv-file-cant-obtain-data/61424 "2011-01-12T01:12:57Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Kamarulnizam\_Rahim](https://avatars.discourse-cdn.com/v4/letter/k/77aa72/32.png) [@Kamarulnizam\_Rahim](https://rubytalk.org/u/Kamarulnizam_Rahim)
#### Post date: [12 January 2011 01:12 UTC](https://rubytalk.org/t/read-csv-file-cant-obtain-data/61424/1 "2011-01-12T01:12:57Z")

</div>

Here is the code i use:

#!/usr/bin/ruby

require 'rubygems'  
require 'csv'

ctr = 0  
@data = []  
CSV.foreach('C:/Users/nizam/test\_q.csv') do |row|  
&nbsp;&nbsp;@data \<\< row  
&nbsp;&nbsp;puts "#{row[0]} | #{row[1]}"  
&nbsp;&nbsp;#unless row[1] =~ /^\[x\]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ctr += 1  
&nbsp;&nbsp;end

puts "#{ctr} rows"

puts "Hello World"

However, the output came like this:  
0 rows  
Hello World

Im pretty sure my .csv file has data in it. But when i run this script,  
non of the data come out. I wonder why??

Thanks  
Nizam

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

### Author: ![Brabuhr](https://avatars.discourse-cdn.com/v4/letter/b/919ad9/32.png) [@Brabuhr](https://rubytalk.org/u/Brabuhr)
#### Post date: [12 January 2011 01:41 UTC](https://rubytalk.org/t/read-csv-file-cant-obtain-data/61424/2 "2011-01-12T01:41:33Z")

</div>

> However, the output came like this:  
> 0 rows  
> Hello World
> 
> Im pretty sure my .csv file has data in it. But when i run this script,  
> non of the data come out. I wonder why??

Can I assume since you asked another question about converting a CSV  
to YAML, you were able to read your file?

> > require 'csv'

=\> true

> > CSV.foreach('foo.csv') do |row|

?\> p row

> > end

["a", "b", "c"]  
["1", "2", "3"]  
["4", "5", "6"]  
["7", "8", "9"]

---

<div class="post-metadata">

### Author: ![Kamarulnizam\_Rahim](https://avatars.discourse-cdn.com/v4/letter/k/77aa72/32.png) [@Kamarulnizam\_Rahim](https://rubytalk.org/u/Kamarulnizam_Rahim)
#### Post date: [12 January 2011 02:11 UTC](https://rubytalk.org/t/read-csv-file-cant-obtain-data/61424/3 "2011-01-12T02:11:20Z")

</div>

No I havnt. I can compile the script with no prob. But there is no  
output. Do you know why? or maybe csv cant be read properly. I stuck  
like few hours on this alone.huhu. Thanks for any help

Nizam

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

### Author: ![Kamarulnizam\_Rahim](https://avatars.discourse-cdn.com/v4/letter/k/77aa72/32.png) [@Kamarulnizam\_Rahim](https://rubytalk.org/u/Kamarulnizam_Rahim)
#### Post date: [12 January 2011 03:42 UTC](https://rubytalk.org/t/read-csv-file-cant-obtain-data/61424/4 "2011-01-12T03:42:18Z")

</div>

YESSSS

At last im able to read the file. It just happen. I copied the old file  
and paste it into my ruby192 folder. It works!!. Attached is the i wish  
to read.  
Thanks for the help guys!! This forum is really helping me a lot

Nizam

Attachments:  
[http://www.ruby-forum.com/attachment/5721/test\_q.csv](http://www.ruby-forum.com/attachment/5721/test_q.csv)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

### Author: ![Justin\_Collins](https://avatars.discourse-cdn.com/v4/letter/j/3e96dc/32.png) [@Justin\_Collins](https://rubytalk.org/u/Justin_Collins)
#### Post date: [12 January 2011 03:26 UTC](https://rubytalk.org/t/read-csv-file-cant-obtain-data/61424/5 "2011-01-12T03:26:15Z")

</div>

Maybe you can provide a test file for people to try with your code?

-Justin

> **···**
>
> On 01/11/2011 06:11 PM, Kamarulnizam Rahim wrote:
> 
> > No I havnt. I can compile the script with no prob. But there is no  
> > output. Do you know why? or maybe csv cant be read properly. I stuck  
> > like few hours on this alone.huhu. Thanks for any help
> > 
> > Nizam

---

<div class="post-metadata">

### Author: ![John\_Morrice](https://avatars.discourse-cdn.com/v4/letter/j/8c91f0/32.png) [@John\_Morrice](https://rubytalk.org/u/John_Morrice)
#### Post date: [12 January 2011 12:34 UTC](https://rubytalk.org/t/read-csv-file-cant-obtain-data/61424/6 "2011-01-12T12:34:59Z")

</div>

> At last im able to read the file. It just happen. I copied the old  
> file and paste it into my ruby192 folder. It works!!. Attached is the  
> i wish to read.  
> Thanks for the help guys!! This forum is really helping me a lot

Thanks! I really like helping people; but you've kind of upset me.

You said you are doing this for a job...

With that in mind, would you agree that you're bloody crazy?

It would be like me putting adverts in the paper offering to sort out  
people's plumbing. I know nothing of pipes! (well... 😉

Sir, what would you have done if no one had replied to your mails?  
Jings man, you'd be buggered!

I'm not saying you're wrong for seeking employment in the booming  
CSV conversion market: oh no, no! Go right ahead.

I just think that you are completely mental.

Anyway, enough BS:

Is your client going to put their files into their ruby192 folder too?

I think you'd better think that one through, matey!
