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
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
}
--
Posted via http://www.ruby-forum.com/.
# 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 Gray II
···
On Dec 6, 2006, at 11:58 PM, Tanushree Bhoi wrote: