How to store data from a CSV file into an array

383 views
Skip to first unread message

Margam

unread,
Jan 8, 2009, 5:48:48 PM1/8/09
to Watir General
Hello all,
I am trying to get data from a particular column (second in my case)
of an CSV file and store it into an array. And then use the array
elements within my script
Curerntly my script(below) uses the "datahandler.rb"
---------------
require 'watir'
require 'watir/datahandler'
require "test/unit"

#Get Data from the CSV file and store in variables. CSV file is in the
same folder as the script
d = DataHandler.new("gmail.csv")

$dataarray=[] #Creating an empty array

#storing each value from the CSV file to the array elements. Note:
"Value" is the header of the second column in the CSV file.
$dataarray[0]=d.data[0].Value
$dataarray[1]=d.data[1].Value
$dataarray[2]=d.data[2].Value
$dataarray[3]=d.data[3].Value
...

#rest of script, where the array element are used.
-------------------
The above approach works. But I don't want to manually enter each
value from the file into each array element.
Is there an easier and better (shorter) way to achieve this?

Please help.
Thank you
Margam

Bill Agee

unread,
Jan 8, 2009, 6:16:57 PM1/8/09
to watir-...@googlegroups.com
The csv module should be able to do what you need, without much fuss:

require 'csv'
array = []
CSV::Reader.parse(File.open('foo.csv')) { |row| array << row[1] }


That snippet will open 'foo.csv', and for each row, push the second
column's value onto the array. Note that if you need to deal with
lines that are commented, empty, or otherwise invalid, a little more
work would be needed to handle those situations.

More info:

http://www.ruby-doc.org/stdlib/libdoc/csv/rdoc/index.html (Click on
the CSV::Reader class for applicable docs)

Margam

unread,
Jan 8, 2009, 6:50:33 PM1/8/09
to Watir General
Hello Bill,
Thank you for your quick reply.
And Thank you once again, as your suggestion works great.

I may have to work on how to deal with invalid lines in the CSV file
in the future. But this works for the moment.

Thank you
Margam

On Jan 8, 3:16 pm, "Bill Agee" <billa...@gmail.com> wrote:
> The csv module should be able to do what you need, without much fuss:
>
> require 'csv'
> array = []
> CSV::Reader.parse(File.open('foo.csv')) { |row| array << row[1] }
>
> That snippet will open 'foo.csv', and for each row, push the second
> column's value onto the array.  Note that if you need to deal with
> lines that are commented, empty, or otherwise invalid, a little more
> work would be needed to handle those situations.
>
> More info:
>
> http://www.ruby-doc.org/stdlib/libdoc/csv/rdoc/index.html(Click on
> the CSV::Reader class for applicable docs)
>

Margam

unread,
Jan 8, 2009, 7:33:31 PM1/8/09
to Watir General
Another method to perform the same function. Just tried using the
"fastercsv" gem and that also works.
Steps
1. install the gem :gem install fastercsv
2. include the following in the script:
require 'fatercsv'
array=[]
FasterCSV.parse(File.open("gmail.csv")){|row| array << row[1]}

This also get the values in the second column of the file "gmail.csv"
and puts it in an array.

Hope this helps, beginners like me.

Margam

Margam

unread,
Jan 11, 2009, 8:28:31 PM1/11/09
to Watir General
Hello Bill,
So I was trying to get data starting from a particular row (instead
of from the first row) and have not been very successful.
Say I want to start getting data from Row 2 and as before Column 2.
I tried the following, but did not work:
CSV::Reader.parse(File.open('gmail.csv')) { |row| array << [1][1] }
CSV::Reader.parse(File.open('gmail.csv')) { |row| array << ['r1c1'] }

Can you help me? How should I be specifying the row and columns to be
saved in the array?
Thank you very much.

Margam

On Jan 8, 3:16 pm, "Bill Agee" <billa...@gmail.com> wrote:
> Thecsvmodule should be able to do what you need, without much fuss:
>
> require 'csv'
> array = []CSV::Reader.parse(File.open('foo.csv')) { |row| array << row[1] }
>
> That snippet will open 'foo.csv', and for each row, push the second
> column's value onto the array.  Note that if you need to deal with
> lines that are commented, empty, or otherwise invalid, a little more
> work would be needed to handle those situations.
>
> More info:
>
> http://www.ruby-doc.org/stdlib/libdoc/csv/rdoc/index.html(Click on
> theCSV::Reader class for applicable docs)
>
> On Thu, Jan 8, 2009 at 2:48 PM, Margam <nk.mar...@gmail.com> wrote:
>
> > Hello all,
> > I am trying to get data from a particular column (second in my case)
> > of anCSVfile and store it into an array. And then use the array
> > elements within my script
> > Curerntly my script(below) uses the "datahandler.rb"
> > ---------------
> > require 'watir'
> > require 'watir/datahandler'
> > require "test/unit"
>
> > #Get Data from theCSVfile and store in variables.CSVfile is in the
> > same folder as the script
> > d = DataHandler.new("gmail.csv")
>
> > $dataarray=[] #Creating an empty array
>
> > #storing each value from theCSVfile to the array elements. Note:

Margam

unread,
Jan 13, 2009, 2:48:53 PM1/13/09
to Watir General
Hello everyone,
Can anyone help with my previous question.
So I am trying to get data starting from a particular row (instead
of from the first row) of a CSV file and have not been very
successful.
Say I want to start getting data from Row 2 and as before Column 2 of
my CSV file.
I tried the following, but did not work:

array=[]
CSV::Reader.parse(File.open('gmail.csv')) { |row| array << [1][1] }
CSV::Reader.parse(File.open('gmail.csv')) { |row| array << ['r1c1'] }

Can anyone help me? How should I be specifying the row and columns to
be
saved in the array?
Thank you very much.
Margam

Bill Agee

unread,
Jan 13, 2009, 3:32:07 PM1/13/09
to watir-...@googlegroups.com
Hey,

One way to do this is to use a counter while iterating over the rows:

require 'csv'
array = []
row_count = 0
CSV::Reader.parse(File.open('foo.csv')) do |row|
row_count += 1
# Don't save the value on the first row
next if (row_count == 1)
array << row[1]
end

That code will increment row_count by 1 at the beginning of each
iteration performed. So as shown, you can use the counter to exit the
block early if you are on row 1.

For more info, I'd suggest checking out the Ruby programming resources
on the Wiki, at:

http://wiki.openqa.org/display/WTR/Learning+Ruby

The Ruby Cheat Sheet document there contains a brief introduction to
iteration, and the other docs go into more depth.

Karthikeyan Margam

unread,
Jan 13, 2009, 5:35:48 PM1/13/09
to watir-...@googlegroups.com
Hello,
Thanks for the snippet. IT works.
But I guess I should have posed my question differently. The new snippet and the first snippet works great to get every value (from second column) from every row  of the CSV file and inserting into an Array.

PLease see the attached CSV file.
My problem is: How do I start grabbing data from a random row:  say row 11 (column 2) and store in an array, instead of starting from the beginning of the CSV file?
Or in other words, Is there a way to specify a particular row to start collecting data using the CSV module?

I hope my question is clear.
Thank you.

Margam
gmail.csv

Walter Kruse

unread,
Jan 14, 2009, 7:51:01 AM1/14/09
to watir-...@googlegroups.com
Hi Margam

You should really read the Ruby documentation on accessing array elements. Nevertheless, here is how I would do it:

require 'csv'

array = []

CSV::Reader.parse(File.open('gmail.csv')) do |row|
  array << row.join(',')
end

puts array[10].split(',')[1] # row 11 (column 2)

puts array[(1 + rand(22))].split(',')[(1 + rand(1))] # truly random, but I don't think that's what you meant

regards
Walter


Date: Tue, 13 Jan 2009 14:35:48 -0800
From: nk.m...@gmail.com
To: watir-...@googlegroups.com
Subject: [wtr-general] Re: How to store data from a CSV file into an array

See all the ways you can stay connected to friends and family
Reply all
Reply to author
Forward
0 new messages