Obtain 'CSV' data

Hi guys,

I have some problem while obtaining data from the csv file format. Here
is the output of the csv file after being loaded in ruby:

"1.a | Maximise use of natural lighting | | Active | | | | | | "
"1.b | Turn-off unnecessary lights/equipment | | Active | | | | |

"

Using the following code:

row_num = 0
@data =
CSV.foreach('ActionPlan.csv') do |row|
  @data << row
  pp "#{row[0]} | #{row[1]} | #{row[2]} | #{row[3]} | #{row[4]} |
#{row[5]} | #{row[6]} | #{row[7]} | #{row[8]} | #{row[9]}"
  #unless row[1] =~ /^\[x\]
        row_num += 1
  end
puts "#{row_num} rows"

My problem is to obtain the 'Active' results and transfer it into
another class which already existed. Do i need to convert the data into
ruby or yaml structures first? (since the purpose of my project is to
produce a yaml file). Thanks in advance

Nizam

···

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

It doesn't look like you really need a CSV parser there. I suggest:

  File.foreach(path) do |line|
    fields = line.split(/\s*\|\s*/)
    # use fields here...
  end

James Edward Gray II

···

On Jan 23, 2011, at 3:23 PM, Kamarulnizam Rahim wrote:

I have some problem while obtaining data from the csv file format. Here
is the output of the csv file after being loaded in ruby:

"1.a | Maximise use of natural lighting | | Active | | | | | | "
"1.b | Turn-off unnecessary lights/equipment | | Active | | | | |
> "

Hello James,

What do you mean by '# use fields here...'? and how to tell ruby that i
need to the 'Active' result from the field? Thanks in advance.

Nizam

···

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

Hi,

When i printed out fields[2], the outputs are nil. This goes for
field[1] and fields[3]. But when i printed out field[0], the outcome is
the same as when i printed out 'fields'. May i know what i did wrong? Im
using the same code as posted by Mr Gray

Nizam

···

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

open("file").each do |line|
  line.chomp!
  fields = line.split(/\s*\|\s*/)
  fields.delete '' # or fields.delete_if {|x| x=~/^\s*$/ }
  fields.each{|x| print "#{x}\n"}
end

···

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

Hi Robert,

This is the result i had when iam using ur code above:

"Issue,Risk/Opportunity,Compliance,Consequence,Current Status,Due
Date,Priority,Responsibility,Probability\n"
["Issue,Risk/Opportunity,Compliance,Consequence,Current Status,Due
Date,Priority,Responsibility,Probability\n"]
nil
"1,Efficiency (Energy),No
Action,Unknown,Neutral,Active,Medium,Possible\n"
["1,Efficiency (Energy),No
Action,Unknown,Neutral,Active,Medium,Possible\n"]
nil

I dont know why it returned value 'nil' instead of 'active'.

Nizam

···

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

"1.a,Maximise use of natural lighting,Active,\n"
["1.a,Maximise use of natural lighting,Active,\n"]
nil
"1.b,Turn-off unnecessary lights/equipment,Active,\n"
["1.b,Turn-off unnecessary lights/equipment,Active,\n"]
nil

···

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

Thanks for the solution. i change the "|" to "," and it worked. But Im
still confuse, because I thought .csv file (data) are separated by "|"
in the first place. I guess im wrong

Nizam

···

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

Kamarulnizam Rahim wrote in post #976986:

Hello James,

What do you mean by '# use fields here...'? and how to tell ruby that i
need to the 'Active' result from the field? Thanks in advance.

Nizam

Result of split is actually an array. So your "Active" column would be
fields[2].

by
TheR

···

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

Are you sure?

10:02:18 ~$ ruby19 x.rb
"1.a | Maximise use of natural lighting | | Active | | | | | | \n"
["1.a", "Maximise use of natural lighting", "", "Active"]
"Active"
"1.b | Turn-off unnecessary lights/equipment | | Active | | | | | | \n"
["1.b", "Turn-off unnecessary lights/equipment", "", "Active"]
"Active"
10:02:19 ~$ cat x.rb

DATA.each do |line|
  a = line.split(/\s*\|\s*/)
  p line, a, a[3]
end

__END__
1.a | Maximise use of natural lighting | | Active | | | | | |
1.b | Turn-off unnecessary lights/equipment | | Active | | | | | |
10:02:25 ~$

Kind regards

robert

···

On Mon, Jan 24, 2011 at 2:14 AM, Kamarulnizam Rahim <niezam54@hotmail.com> wrote:

When i printed out fields[2], the outputs are nil. This goes for
field[1] and fields[3]. But when i printed out field[0], the outcome is
the same as when i printed out 'fields'. May i know what i did wrong? Im
using the same code as posted by Mr Gray

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

Because you said the fields were separated by '|' and this is why
James and Robert were doing this:

line.split(/\s*\|\s*/) # split using | as the separator

but now it seems your fields are separated by commas, which should
require something like:

line.split(/\s*,\s*/)

Which one is it: commas or pipes?

Jesus.

···

On Mon, Jan 24, 2011 at 10:55 PM, Kamarulnizam Rahim <niezam54@hotmail.com> wrote:

Hi Robert,

This is the result i had when iam using ur code above:

"Issue,Risk/Opportunity,Compliance,Consequence,Current Status,Due
Date,Priority,Responsibility,Probability\n"
["Issue,Risk/Opportunity,Compliance,Consequence,Current Status,Due
Date,Priority,Responsibility,Probability\n"]
nil
"1,Efficiency (Energy),No
Action,Unknown,Neutral,Active,Medium,Possible\n"
["1,Efficiency (Energy),No
Action,Unknown,Neutral,Active,Medium,Possible\n"]
nil

I dont know why it returned value 'nil' instead of 'active'.

The C in CSV stands for "comma."

James Edward Gray II

···

On Jan 25, 2011, at 3:55 PM, Kamarulnizam Rahim wrote:

Thanks for the solution. i change the "|" to "," and it worked. But Im
still confuse, because I thought .csv file (data) are separated by "|"
in the first place. I guess im wrong