Obtain 'CSV' data

K

Kamarulnizam Rahim

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
 
J

James Edward Gray II

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 Gray II
 
K

Kamarulnizam Rahim

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
 
D

Damjan Rems

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
 
K

Kamarulnizam Rahim

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
 
S

Steel Steel

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
 
R

Robert Klemme

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
 
K

Kamarulnizam Rahim

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
 
K

Kamarulnizam Rahim

"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
 
J

Jesús Gabriel y Galán

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

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

Kamarulnizam Rahim

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
 
J

James Edward Gray II

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 Gray II
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top