[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

csv help

charlie bowman

1/27/2006 9:18:00 PM

Can anyone point me to a good tutorial on csv files and ruby. I've read
the docs that go along with the standard csv library but it's hard to
put it all together. I'm trying to write a simple timekeeper application
using csv files but I don't really know where to start. Maybe someone
can point out the many flaws in this beginners attempt. Currently all
I'm trying to do is create a row of time data each time the app is ran.

require 'csv'
class TimeKeeper

## constant of where the csv file is stored
@@csv_file = "/shared/home/cmbowma/.timeclock"

def initialize
end

def read
@data = String.new
CSV.parse open(@@csv_file).read do |row|
@data << row.to_s << ','
end
@data
end

def write(data)
writer = CSV.open(@@csv_file, 'w')
writer << data
writer.close
end
end

csv = TimeKeeper.new
@data = csv.read.to_a
time = Time.now
@data << [time]
csv.write(@data)

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


3 Answers

James Gray

1/27/2006 9:25:00 PM

0

On Jan 27, 2006, at 3:18 PM, charlie bowman wrote:

> def read
> @data = String.new
> CSV.parse open(@@csv_file).read do |row|
> @data << row.to_s << ','
> end
> @data
> end

To read a CSV file into an Array, try:

array_of_csv_rows = CSV.read("my_file.csv")

If you want to read CSV row by row, use:

CSV.foeach("my_file.csv") do |row
# use row here
end

Hope that helps.

James Edward Gray II



Gene Tani

1/27/2006 10:00:00 PM

0


charlie bowman wrote:
> Can anyone point me to a good tutorial on csv files and ruby. I've read
> the docs that go along with the standard csv library but it's hard to
> put it all together.

Here's some stuff to look at:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...
http://redhanded.hobix.com/bits/howDoYouParseTabSeparatedV...
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...

pickax 2, page 663 discusses CSV lib

http://rubyforge.org/projects/...
http://www.linuxdevcenter.com/pub/a/linux/2003/09/18/ruby_csv.h...

The Peeler

6/30/2013 10:22:00 AM

0

On Sat, 29 Jun 2013 16:54:10 -0700, The Rectum, the resident psychopath of
sci and scj, FAKING his time zone again and IMPERSONATING his master, The
Peeler, wrote:

>> >> >> >> >> I'm your master, and you are everyone's asshole on Usenet, psycho! <BG>
>> >> >> >> >
>> >> >> >> > INNIT The Grik anus! <GB>
>> >> >> >>
>> >> >> >> It IS, poor sod! <BG>
>> >> >> >
>> >> >> > INNIT very much innit! <GB>
>> >> >>
>> >> >> VEEERY much, poor idiot! LOL
>> >> >
>> >> > AND too The Grik anus! LOL3
>> >>
>> >> OK, poor sod, try that in ENGLISH again! <BG>
>> >
>> > Will, you The Grik anus? <GB>
>>
>> IOW, you don't manage to do so, right, you fucked up stupid Serbian swine!
>> LOL
>
> What, the fuck kind, of ridiculous retarded pidginese is, THAT?

<BG> Is your illiterate Serbian "mind" overtaxed again? No surprise here!
LOL

--
Retarded, anal, subnormal and extremely proud of it: our resident
psychopath, The Retard (aka "The Rectum").