# \#help in reading from csv file

**URL:** <https://www.ruby-forum.com/t/help-in-reading-from-csv-file/78604>\
**Category:** Ruby\
**Created:** [December 7, 2006, 6:59am UTC](https://www.ruby-forum.com/t/help-in-reading-from-csv-file/78604 "2006-12-07T06:59:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sampurna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9de053/32.png) [@sampurna](https://www.ruby-forum.com/u/sampurna)\
**Post date:** [December 7, 2006, 6:59am UTC](https://www.ruby-forum.com/t/help-in-reading-from-csv-file/78604/1 "2006-12-07T06:59:34Z")

</div>

Hi Friends,

Here I am trying to read from a cvs file

my code is like this

require ‘nebo’ # my ruby library where the function create\_product is  
defined

product=CSV.open(‘createproduct.csv’,‘r’) #reading data from csv file

loginnebo=CSV.open(‘login.csv’,‘r’) #file to get the creadential to  
login  
to the application

login1=loginnebo.shift

login(login1)

# iterate to the csv file row by row till there is any row exists.

while(row=product.shift)  
create\_product(row)  
end

logout

Here in this code it is reading one extra empty row from the file how to  
handle that in ruby.  
I want to control the loop where there is no more rows in the csv file.  
Can anybody helps.Help will be highly appreciated.

Thanks in advance  
Sampurna

---

<div class="post-metadata">

**Author:** ![sampurna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9de053/32.png) [@sampurna](https://www.ruby-forum.com/u/sampurna)\
**Post date:** [December 7, 2006, 11:35am UTC](https://www.ruby-forum.com/t/help-in-reading-from-csv-file/78604/2 "2006-12-07T11:35:45Z")

</div>

Here how you can access correctly a csv file. This works and is more in  
the ruby style.

require ‘csv’

CSV::Reader.parse(File.open(‘my.csv’, ‘rb’)) do |row|  
next if row.first.nil? #skip blank lines  
create\_product(row)  
end

But you can be more restrictive by changing the condition on the next  
by:

next unless row.size == 5 #skip all lines without the exact number of  
columns (5 here)

I hope this will help you,

++  
jerome () {  
c = Woa! Kft  
w = [http://www.woa.hu](http://www.woa.hu)  
}

---

<div class="post-metadata">

**Author:** ![sampurna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9de053/32.png) [@sampurna](https://www.ruby-forum.com/u/sampurna)\
**Post date:** [December 7, 2006, 3:26pm UTC](https://www.ruby-forum.com/t/help-in-reading-from-csv-file/78604/3 "2006-12-07T15:26:58Z")

</div>

On Dec 6, 2006, at 11:58 PM, Tanushree B. wrote:

> # iterate to the csv file row by row till there is any row exists.
> 
> while(row=product.shift)  
> create\_product(row)

create\_product(row) unless row.empty?

> end

> Here in this code it is reading one extra empty row from the file  
> how to  
> handle that in ruby.  
> I want to control the loop where there is no more rows in the csv  
> file.  
> Can anybody helps.Help will be highly appreciated.

I suspect there is an empty line in your file. If so, the above fix  
should help.

James Edward G. II
