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

On Jan 23, 2011, at 3:23 PM, Kamarulnizam R. 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 | | | | |
| "

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 G. II

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

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

Kamarulnizam R. 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

On Mon, Jan 24, 2011 at 2:14 AM, Kamarulnizam R.
[email protected] 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

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

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

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

On Mon, Jan 24, 2011 at 10:55 PM, Kamarulnizam R.
[email protected] wrote:

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’.

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.

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

On Jan 25, 2011, at 3:55 PM, Kamarulnizam R. 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

The C in CSV stands for “comma.”

James Edward G. II

“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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs