Read thru Csv file and store it in variables

HI All,
        I need to read thru Excel file saved as CSV. Exclude the header
and store the data in variables.
Based on the number of records the script has to go thru that number of
iterations.

I tried just s simple faster_csv code, which isnt working!!
<code>
require 'rubygems'
require 'faster_csv'

path = "C:/Users/test/Desktop/data.txt"

FasterCSV.foreach(path) do |row|
    puts row

  end
<code>

<Error>
C:/Program
Files/Ruby/lib/ruby/gems/1.9.1/gems/fastercsv-1.5.4/lib/faster_csv.rb:13:in
`const_missing': Please switch to Ruby 1.9's standard CSV library. It's
FasterCSV plus support for Ruby 1.9's m17n encoding engine.
(NotImplementedError)
<ERROR>

I am not aware of this error, Can any of you plz recommend better way to
achieve the result.

Thanks

···

--
Posted via http://www.ruby-forum.com/.

In Ruby 1.9, fasterCSV has been made the implementation of the stdlib
CSV, so you should be able to remove the rubygems (which btw, is also
included by default in 1.9) and just make require 'csv' and CSV
instead of FasterCSV.

http://ruby-doc.org/stdlib/libdoc/csv/rdoc/index.html

Jesus.

···

On Wed, Oct 5, 2011 at 7:25 PM, ideal one <idealone5@hotmail.com> wrote:

HI All,
I need to read thru Excel file saved as CSV. Exclude the header
and store the data in variables.
Based on the number of records the script has to go thru that number of
iterations.

I tried just s simple faster_csv code, which isnt working!!
<code>
require 'rubygems'
require 'faster_csv'

path = "C:/Users/test/Desktop/data.txt"

FasterCSV.foreach(path) do |row|
puts row

end
<code>

<Error>
C:/Program
Files/Ruby/lib/ruby/gems/1.9.1/gems/fastercsv-1.5.4/lib/faster_csv.rb:13:in
`const_missing': Please switch to Ruby 1.9's standard CSV library. It's
FasterCSV plus support for Ruby 1.9's m17n encoding engine.
(NotImplementedError)
<ERROR>

I am not aware of this error, Can any of you plz recommend better way to
achieve the result.

Maybe using bash before using ruby.

···

2011/10/5 ideal one <idealone5@hotmail.com>

HI All,
       I need to read thru Excel file saved as CSV. Exclude the header
and store the data in variables.
Based on the number of records the script has to go thru that number of
iterations.

I tried just s simple faster_csv code, which isnt working!!
<code>
require 'rubygems'
require 'faster_csv'

path = "C:/Users/test/Desktop/data.txt"

FasterCSV.foreach(path) do |row|
   puts row

end
<code>

<Error>
C:/Program
Files/Ruby/lib/ruby/gems/1.9.1/gems/fastercsv-1.5.4/lib/faster_csv.rb:13:in
`const_missing': Please switch to Ruby 1.9's standard CSV library. It's
FasterCSV plus support for Ruby 1.9's m17n encoding engine.
(NotImplementedError)
<ERROR>

I am not aware of this error, Can any of you plz recommend better way to
achieve the result.

Thanks

--
Posted via http://www.ruby-forum.com/\.

--
Analista de Sistemas
*MBA em Logística<http://www.logisticadescomplicada.com/a-auto-regulacao-dos-servicos-de-transporte-publico-urbano-de-passageiros/&gt;,
Mobilização e Meio Ambiente/GETRAM – Gerência Executiva de Transportes e
Mobilização*
*
*
Provedor <http://www.InstitutoFederalista.com/debate/&gt; de Serviços na
InterNet

Atenção: Esta carta pode conter anexos no formato *ODF* (*Open Document
Format*)/*ABNT* (extensões *odt*, *ods*, *odp*, *odb*, *odg*). Antes de
pedir os anexos em outro formato, você pode instalar gratuita e livremente
o *BrOffice* (http://www.broffice.org) ou o seguinte Aditivo para Microsoft
Office (R) (Hardware | Oracle).

"Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> wrote in post
#1025175:

···

On Wed, Oct 5, 2011 at 7:25 PM, ideal one <idealone5@hotmail.com> wrote:

Files/Ruby/lib/ruby/gems/1.9.1/gems/fastercsv-1.5.4/lib/faster_csv.rb:13:in
`const_missing': Please switch to Ruby 1.9's standard CSV library. It's
FasterCSV plus support for Ruby 1.9's m17n encoding engine.
(NotImplementedError)
<ERROR>

I am not aware of this error, Can any of you plz recommend better way to
achieve the result.

In Ruby 1.9, fasterCSV has been made the implementation of the stdlib
CSV, so you should be able to remove the rubygems (which btw, is also
included by default in 1.9) and just make require 'csv' and CSV
instead of FasterCSV.

http://ruby-doc.org/stdlib/libdoc/csv/rdoc/index.html

Jesus.

Hi there,
           I did implement the csv using hash, but Is there a way i can
get data for each iteration from Hash.

My Input
---------
Name,Account,T1,T2,Date
test1,1,QA1,PQ1,10-Sep
test2,2,QA2,PQ2,11-Sep
test3,3,QA3,PQ3,12-Sep

eg: In my Iteration, I want to utilize each

1st row:
Run Program
Name=test1
Account=1
T1=QA1
end

row2 values
Run Program
Name=test2
Account=2
T1=QA2
end

so on, till the end of the file.

<Code>
require 'csv'
path = "C:/Users/test/Desktop/data.txt"

csv_data = CSV.read(path)
headers = csv_data.shift.map {|i| i.to_s }

data1 = csv_data.map {|row| row.map {|cell| cell.to_s } }
hashe1 = string_data.map {|row| Hash[*headers.zip(row).flatten] }
puts "Done"
<code>

--
Posted via http://www.ruby-forum.com/\.

I don't really understand what do you want. You want to create a hash
in each iteration with the keys being the headers and the values being
the value in each row?
For example:

irb(main):003:0> CSV.foreach("data.txt", {:headers => true}) do |row|
irb(main):004:1* p row.to_hash
irb(main):005:1> end
{"name"=>"test1", "account"=>"1", "date"=>"10-Sep", "t1"=>"QA1", "t2"=>"PQ1"}
{"name"=>"test2", "account"=>"2", "date"=>"11-Sep", "t1"=>"QA2", "t2"=>"PQ2"}
{"name"=>"test3", "account"=>"3", "date"=>"12-Sep", "t1"=>"QA3", "t2"=>"PQ3"}
=> nil

This is my data.txt:

name,account,t1,t2,date
test1,1,QA1,PQ1,10-Sep
test2,2,QA2,PQ2,11-Sep
test3,3,QA3,PQ3,12-Sep

Maybe you can use the values directly, but if you need a hash that's
how I'd do it.

Hope this helps,

Jesus.

···

On Wed, Oct 5, 2011 at 11:14 PM, ideal one <idealone5@hotmail.com> wrote:

"Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> wrote in post
#1025175:

On Wed, Oct 5, 2011 at 7:25 PM, ideal one <idealone5@hotmail.com> wrote:

Files/Ruby/lib/ruby/gems/1.9.1/gems/fastercsv-1.5.4/lib/faster_csv.rb:13:in
`const_missing': Please switch to Ruby 1.9's standard CSV library. It's
FasterCSV plus support for Ruby 1.9's m17n encoding engine.
(NotImplementedError)
<ERROR>

I am not aware of this error, Can any of you plz recommend better way to
achieve the result.

In Ruby 1.9, fasterCSV has been made the implementation of the stdlib
CSV, so you should be able to remove the rubygems (which btw, is also
included by default in 1.9) and just make require 'csv' and CSV
instead of FasterCSV.

http://ruby-doc.org/stdlib/libdoc/csv/rdoc/index.html

Jesus.

Hi there,
I did implement the csv using hash, but Is there a way i can
get data for each iteration from Hash.

My Input
---------
Name,Account,T1,T2,Date
test1,1,QA1,PQ1,10-Sep
test2,2,QA2,PQ2,11-Sep
test3,3,QA3,PQ3,12-Sep

eg: In my Iteration, I want to utilize each

1st row:
Run Program
Name=test1
Account=1
T1=QA1
end

row2 values
Run Program
Name=test2
Account=2
T1=QA2
end

so on, till the end of the file.

<Code>
require 'csv'
path = "C:/Users/test/Desktop/data.txt"

csv_data = CSV.read(path)
headers = csv_data.shift.map {|i| i.to_s }

data1 = csv_data.map {|row| row.map {|cell| cell.to_s } }
hashe1 = string_data.map {|row| Hash[*headers.zip(row).flatten] }
puts "Done"
<code>

"Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> wrote in post
#1025230:

I am not aware of this error, Can any of you plz recommend better way to

Run Program

csv_data = CSV.read(path)
headers = csv_data.shift.map {|i| i.to_s }

data1 = csv_data.map {|row| row.map {|cell| cell.to_s } }
hashe1 = string_data.map {|row| Hash[*headers.zip(row).flatten] }
puts "Done"
<code>

I don't really understand what do you want. You want to create a hash
in each iteration with the keys being the headers and the values being
the value in each row?
For example:

irb(main):003:0> CSV.foreach("data.txt", {:headers => true}) do |row|
irb(main):004:1* p row.to_hash
irb(main):005:1> end
{"name"=>"test1", "account"=>"1", "date"=>"10-Sep", "t1"=>"QA1",
"t2"=>"PQ1"}
{"name"=>"test2", "account"=>"2", "date"=>"11-Sep", "t1"=>"QA2",
"t2"=>"PQ2"}
{"name"=>"test3", "account"=>"3", "date"=>"12-Sep", "t1"=>"QA3",
"t2"=>"PQ3"}
=> nil

This is my data.txt:

name,account,t1,t2,date
test1,1,QA1,PQ1,10-Sep
test2,2,QA2,PQ2,11-Sep
test3,3,QA3,PQ3,12-Sep

Maybe you can use the values directly, but if you need a hash that's
how I'd do it.

Hope this helps,

Jesus.

Hello,
     Thanks for your Info, Sorry if i am not clear with my requirement.
Let me clearly explain below: I am looking for DATA DRIVEN Scenario.

I read CSV file and use that data in my code. ie If i have 10 rows, i
will run my program 10 times.

example:
<MyProgram>

def testprog
type name
type account
type t1
type t2
type date
end

<MyProgram>

i will have to iterate each time into my code and use different row
values, its sort of Data Driven Automation.
let me know if my requirement makes sense.

Cheers

···

On Wed, Oct 5, 2011 at 11:14 PM, ideal one <idealone5@hotmail.com> > wrote:

--
Posted via http://www.ruby-forum.com/\.

I'm not really sure. Something like this maybe (where type is
something meaningful for you)?

def my_prog values
  type values["name"]
  type values["account"]
  type values["t1"]
  type values["t2"]
  type values["date"]
end

CSV.foreach("data.txt", {:headers => true}) do |row|
  my_prog row.to_hash
end

Jesus.

···

On Thu, Oct 6, 2011 at 1:24 AM, ideal one <idealone5@hotmail.com> wrote:

"Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> wrote in post
#1025230:

On Wed, Oct 5, 2011 at 11:14 PM, ideal one <idealone5@hotmail.com> >> wrote:

I am not aware of this error, Can any of you plz recommend better way to

Run Program

csv_data = CSV.read(path)
headers = csv_data.shift.map {|i| i.to_s }

data1 = csv_data.map {|row| row.map {|cell| cell.to_s } }
hashe1 = string_data.map {|row| Hash[*headers.zip(row).flatten] }
puts "Done"
<code>

I don't really understand what do you want. You want to create a hash
in each iteration with the keys being the headers and the values being
the value in each row?
For example:

irb(main):003:0> CSV.foreach("data.txt", {:headers => true}) do |row|
irb(main):004:1* p row.to_hash
irb(main):005:1> end
{"name"=>"test1", "account"=>"1", "date"=>"10-Sep", "t1"=>"QA1",
"t2"=>"PQ1"}
{"name"=>"test2", "account"=>"2", "date"=>"11-Sep", "t1"=>"QA2",
"t2"=>"PQ2"}
{"name"=>"test3", "account"=>"3", "date"=>"12-Sep", "t1"=>"QA3",
"t2"=>"PQ3"}
=> nil

This is my data.txt:

name,account,t1,t2,date
test1,1,QA1,PQ1,10-Sep
test2,2,QA2,PQ2,11-Sep
test3,3,QA3,PQ3,12-Sep

Maybe you can use the values directly, but if you need a hash that's
how I'd do it.

Hope this helps,

Jesus.

Hello,
Thanks for your Info, Sorry if i am not clear with my requirement.
Let me clearly explain below: I am looking for DATA DRIVEN Scenario.

I read CSV file and use that data in my code. ie If i have 10 rows, i
will run my program 10 times.

example:
<MyProgram>

def testprog
type name
type account
type t1
type t2
type date
end

<MyProgram>

i will have to iterate each time into my code and use different row
values, its sort of Data Driven Automation.
let me know if my requirement makes sense.

You can directly assign fields to block parameters and have your code
inside the foreach loop:

13:27:13 Temp$ ./csv.rb data.csv
name=James country=USA
name=Robert country=Germany
name=Michael country=Neverland
13:27:18 Temp$ cat data.csv
James,bar,USA
Robert,bbb,Germany
Michael,ignore,Neverland
13:27:27 Temp$ cat -n csv.rb
     1 #!/bin/env ruby19
     2
     3 require 'csv'
     4
     5 CSV.foreach ARGV.shift do |name, ign1, country|
     6 printf "name=%-30s country=%-30s\n", name, country
     7 end
13:28:18 Temp$

Of course you can invoke any method from there as well.

Kind regards

robert

···

On Thu, Oct 6, 2011 at 1:24 AM, ideal one <idealone5@hotmail.com> wrote:

Thanks for your Info, Sorry if i am not clear with my requirement\.

Let me clearly explain below: I am looking for DATA DRIVEN Scenario.

I read CSV file and use that data in my code. ie If i have 10 rows, i
will run my program 10 times.

i will have to iterate each time into my code and use different row
values, its sort of Data Driven Automation.
let me know if my requirement makes sense.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Re: How do I read, split and derive from CSV using bash script?

I have a CSV file that I need to obtain values from... have absolutely
NO idea how to do it at all, except for:

stuff=`cat ./directory_setup/directory_setup.csv`

From there I'm lost. I need to then do the PHP equivalent of
fgetscsv() to get a specific column field from the parsed CSV file, or
even the TCL equivalent of split/list/split to parse it and retrieve
it.

How is it done though in Bash? I am ultimately trying to dynamically
obtain the document root which is entered as a value in a CSV file
(have no other known way of ever knowing that in the scope of the
portable/scalable script I'm writing)

This function splits a CSV line into its components, and stores
them in an array called values:

csv_split() { ## USAGE: csv_split CSV_RECORD
local record=${1%"${CR}"}
local right
local vnum=0
unset values
while [ -n "$record" ]
do
case $record in
\"*) right=${record#*\",}
value=${record%%\",*}
values[$vnum]=${value#\"}
;;
*) values[$vnum]=${record%%,*}
right=${record#*,}
;;
esac
case $record in
*,*) record=${right} ;;
*) record=${record#\"}
values[$vnum]=${record%\"}
break;;
esac
vnum=$(( $vnum + 1 ))
done
}

N=3
CR=$'\r'
while IFS= read -r line
do
csv_split "$line"
printf "%s\n" "${values[N]}" ## print field N (numbered from 0)
done < ./directory_setup/directory_setup.csv

···

On Sun, 25 Jan 2004 at 03:01 GMT, Phil Powell wrote:

--
Chris F.A. Johnson http://cfaj.freeshell.org
================================================== =================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License

2011/10/7 Robert Klemme <shortcutter@googlemail.com>

On Thu, Oct 6, 2011 at 1:24 AM, ideal one <idealone5@hotmail.com> wrote:

> Thanks for your Info, Sorry if i am not clear with my requirement.
> Let me clearly explain below: I am looking for DATA DRIVEN Scenario.
>
> I read CSV file and use that data in my code. ie If i have 10 rows, i
> will run my program 10 times.

> i will have to iterate each time into my code and use different row
> values, its sort of Data Driven Automation.
> let me know if my requirement makes sense.

You can directly assign fields to block parameters and have your code
inside the foreach loop:

13:27:13 Temp$ ./csv.rb data.csv
name=James country=USA
name=Robert country=Germany
name=Michael country=Neverland
13:27:18 Temp$ cat data.csv
James,bar,USA
Robert,bbb,Germany
Michael,ignore,Neverland
13:27:27 Temp$ cat -n csv.rb
    1 #!/bin/env ruby19
    2
    3 require 'csv'
    4
    5 CSV.foreach ARGV.shift do |name, ign1, country|
    6 printf "name=%-30s country=%-30s\n", name, country
    7 end
13:28:18 Temp$

Of course you can invoke any method from there as well.

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

--
Analista de Sistemas
*MBA em Logística<http://www.logisticadescomplicada.com/a-auto-regulacao-dos-servicos-de-transporte-publico-urbano-de-passageiros/&gt;,
Mobilização e Meio Ambiente/GETRAM – Gerência Executiva de Transportes e
Mobilização*
*
*
Provedor <http://www.InstitutoFederalista.com/debate/&gt; de Serviços na
InterNet

Atenção: Esta carta pode conter anexos no formato *ODF* (*Open Document
Format*)/*ABNT* (extensões *odt*, *ods*, *odp*, *odb*, *odg*). Antes de
pedir os anexos em outro formato, você pode instalar gratuita e livremente
o *BrOffice* (http://www.broffice.org) ou o seguinte Aditivo para Microsoft
Office (R) (Hardware | Oracle).