[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Deleting row from CSV

robnewnham

6/26/2008 10:58:00 AM

I have come code which is looking for a particular number within a CSV
row (item no 2). If it finds it, I want it to output the row
unchanged - but if it isn't there, I want it to delete the row
entirely.

I can't get the following code to work - can anyone spot my mistakes?

reequire "csv"
def basketExtract
f = File.open('DATA.DAT')
csvr = CSV::Reader.create(f,'|')
# import CSV file and read it.

header = csvr.shift
#removes header character
outfile = File.new('NEWDATA.DAT','w')
CSV::Writer.create(outfile, '|') << header

csvr.each do |csv|
# read each row in the CSV
if csv[1] == 699 #if the branch number is one of the requested
ones.
outrow = csv
else #if it isn't one in the list
next
end
CSV::Writer.create(outfile, '|') << outrow
end
end
5 Answers

Jesús Gabriel y Galán

6/26/2008 12:52:00 PM

0

On Thu, Jun 26, 2008 at 12:57 PM, <robnewnham@gmail.com> wrote:
> I have come code which is looking for a particular number within a CSV
> row (item no 2). If it finds it, I want it to output the row
> unchanged - but if it isn't there, I want it to delete the row
> entirely.
>
> I can't get the following code to work - can anyone spot my mistakes?
>
> reequire "csv"
> def basketExtract
> f = File.open('DATA.DAT')
> csvr = CSV::Reader.create(f,'|')
> # import CSV file and read it.
>
> header = csvr.shift
> #removes header character
> outfile = File.new('NEWDATA.DAT','w')
> CSV::Writer.create(outfile, '|') << header
>
> csvr.each do |csv|
> # read each row in the CSV
> if csv[1] == 699 #if the branch number is one of the requested
> ones.

I think the entries are strings, so this comparison will never be true.
Try

if csv[1] == "699"

> outrow = csv
> else #if it isn't one in the list
> next
> end
> CSV::Writer.create(outfile, '|') << outrow
> end
> end
>
>

On the other hand, I would use fastercsv and do something like:

require 'fastercsv'

FasterCSV.open("new.csv", "w") do |out|
FasterCSV.foreach("test.csv", {:headers => true, :return_headers =>
true, :col_sep => '|'}) do |row|
out << row if (row.header_row? || row[1] == "699")
end
end

Hope this helps,

Jesus.

Salil Gaikwad

2/27/2009 2:17:00 PM

0

where shold i get the API of all the classes and methods fof fastercsv.
I API found in the http://www.ruby-doc... but i couldn't find
fastercsv in it.

Thanx in Advance
Salil
--
Posted via http://www.ruby-....

James Gray

2/27/2009 3:47:00 PM

0

On Feb 27, 2009, at 8:16 AM, Salil Gaikwad wrote:

> where shold i get the API of all the classes and methods fof
> fastercsv.

http://fastercsv.ruby...

James Edward Gray II


László

2/27/2009 5:43:00 PM

0

I installed the Ruby 1.9.1, but I am new in Ruby.
I would like plot geometry on screen, for example circle, triangle, etc ...
and make a website with

whiteboard program. Please help me orientate myself. In library I found
only tk. but I am not sure, that I can

only a point to draw to screen.

And I saw some draw program, but wich will working with 1.9.1. ?



Laszlo


László

2/27/2009 9:01:00 PM

0

Hello,

I have installed the Ruby 1.9.1, but I am new in Ruby.
I would like plot geometry on screen, for example circle, triangle, etc ...
and make a website with whiteboard program. Please help me orientate
myself. In library I found
only tk. with a few program, but I am not sure, that I can only a point to
draw to screen.

And I saw some draw program, but wich will working with 1.9.1. on windows
XP ?


Thank you Laszlo