Re: Newbie: fastcsv: Read rows, print rows



On Jun 29, 2007, at 1:50 PM, drubdrub@xxxxxxxxx wrote:

New to Ruby. It probably shows.

Welcome to Ruby!

Trying to accomplish some simple initial tasks, and study.

Objectives
1. Read rows from a CSV file
2. Write rows to STDOUT

A cat() equivalent.

I've been looking but don't find it. Are there any good tutorials on
using fastcsv? This might be a good candidate for such.

There's the documentation:

http://fastercsv.rubyforge.org/

You'll find basic reading and writing information on this page in particular:

http://fastercsv.rubyforge.org/classes/FasterCSV.html

There are also some examples in the source:

http://viewvc.rubyforge.mmmultiworks.com/cgi/viewvc.cgi/trunk/ examples/?root=fastercsv

require
'fastercsv'

The above should be on one line. You may also need to add a:

require 'rubygems'

before requiring FasterCSV, depending on your environment.

fastercsv.open("rfile2", "r") do |csv|

There is an error is on the line above, which needs to be:

FasterCSV.open...

csvData.each{|row| puts "row: ${row}"}

Change csvData to csv in the line above. Also replace ${row} with # {row.inspect}.

end

That should get you running.

You can do this a little easier using foreach():

FCSV.foreach("rfile2") do |row|
p row
end

Hope that helps.

James Edward Gray II


.



Relevant Pages

  • Re: help -in grouping the data
    ... > I have a csv file: ... You need to read in your file into an array. ... The best material model of a cat is another, or preferably the same, cat. ...
    (comp.soft-sys.matlab)
  • Newbie: fastcsv: Read rows, print rows
    ... New to Ruby. ... Trying to accomplish some simple ... Read rows from a CSV file ... Write rows to STDOUT ...
    (comp.lang.ruby)
  • Re: Extract a field in a string collection
    ... From a .csv file, I want to extract the last field (say it's $15, or, I ... the AWK will sent the values to. ... None of this keeps the comma separator. ... scott@thinkpad:~> cat foo.awk ...
    (comp.lang.awk)
  • generating stats from stdout on a regular interval
    ... I've got an executable which writes out data to stdout in a CSV sort ... stats out to a CSV file. ... The popen2 module can read the stdout just like a file, ...
    (comp.lang.python)
  • Re: Importing CVS files
    ... > Depending on what your end goal is, you can import it directly into a ... >> I'm wondering if there's any quick way to extract data from a CSV file ... > the latter, cheers, just looking for some quick pointers in the right ...
    (microsoft.public.dotnet.languages.csharp)