Newbie: fastcsv: Read rows, print rows

Discussion in 'Ruby' started by drubdrub@gmail.com, Jun 29, 2007.

  1. Guest

    New to Ruby. It probably shows. 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.

    Many thanks!


    require
    'fastercsv'

    fastercsv.open("rfile2", "r") do |csv|
    csvData.each{|row| puts "row: ${row}"}
    end
    , Jun 29, 2007
    #1
    1. Advertising

  2. On Jun 29, 2007, at 1:50 PM, 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
    James Edward Gray II, Jun 29, 2007
    #2
    1. Advertising

  3. Guest

    Re: Newbie: fastcsv: Read rows, print rows

    Thanks for such a rapid, patient response!

    You certainly helped me get "unstuck". Appreciate the references. I
    had found the doc pages but was still laboring.

    For the record, here is my working code. No rocket science, but may
    be useful to someone.

    All the best!

    <code>
    require 'rubygems'
    require 'fastercsv'

    infile = "infile2.csv"

    FasterCSV.open(infile, "r") do |row|
    row.each{|row| puts "row: #{row.inspect}"}
    end
    </
    code>

    ----------------------------------

    On Jun 29, 12:06 pm, James Edward Gray II <>
    wrote:
    > On Jun 29, 2007, at 1:50 PM, 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
    , Jun 29, 2007
    #3
  4. Re: Newbie: fastcsv: Read rows, print rows

    wrote:
    > Thanks for such a rapid, patient response!
    >
    > You certainly helped me get "unstuck". Appreciate the references. I
    > had found the doc pages but was still laboring.
    >
    > For the record, here is my working code. No rocket science, but may
    > be useful to someone.
    >
    > All the best!
    >
    > <code>
    > require 'rubygems'
    > require 'fastercsv'
    >
    > infile = "infile2.csv"
    >
    > FasterCSV.open(infile, "r") do |row|
    > row.each{|row| puts "row: #{row.inspect}"}
    > end
    > </
    > code>


    Just be aware about the fact that row is changed after row.each

    > irb(main):001:0> a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    > => [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    > irb(main):002:0> a.each do |row|
    > irb(main):003:1* row.each {|row| p row}
    > irb(main):004:1> p row
    > irb(main):005:1> end
    > 1
    > 2
    > 3
    > 3
    > 4
    > 5
    > 6
    > 6
    > 7
    > 8
    > 9
    > 9
    > => [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    Stefan Mahlitz, Jul 2, 2007
    #4
  5. drub Guest

    Re: Newbie: fastcsv: Read rows, print rows


    >
    > Just be aware about the fact that row is changed after row.each


    Thanks for the warning!

    There must be a fastercsv feature to get rid of the encapsulating '
    characters (e.g. the single quote).

    FasterCSV.open(infile, "r") do |row|

    row.each do |
    field|
    field.each do |field|
    print "|"
    print "#{field}"


    end
    puts "|"
    end
    end

    The output:
    |'field 1'|'field 2'|'field 3'|

    I want:
    |field 1|field 2|field 3|
    drub, Jul 16, 2007
    #5
  6. Re: Newbie: fastcsv: Read rows, print rows

    On Jul 16, 2007, at 5:53 PM, drub wrote:

    >
    >>
    >> Just be aware about the fact that row is changed after row.each

    >
    > Thanks for the warning!
    >
    > There must be a fastercsv feature to get rid of the encapsulating '
    > characters (e.g. the single quote).
    >
    > FasterCSV.open(infile, "r") do |row|
    >
    > row.each do |field|
    > field.each do |field|
    > print "|"
    > print "#{field}"
    >
    >
    > end
    > puts "|"
    > end
    > end
    >
    > The output:
    > |'field 1'|'field 2'|'field 3'|
    >
    > I want:
    > |field 1|field 2|field 3|


    The single quote character is not part of the CSV specification, so
    they are not removed. I'm afraid you will need to pluck those out by
    hand.

    James Edward Gray II
    James Edward Gray II, Jul 17, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Subba Rao via DotNetMonster.com

    script for moving rows up and down and traverse thru rows of HTML table

    Subba Rao via DotNetMonster.com, Mar 19, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    8,196
    Subba Rao via DotNetMonster.com
    Mar 19, 2005
  2. keto
    Replies:
    0
    Views:
    898
  3. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    335
    David Cournapeau
    Dec 30, 2008
  4. Replies:
    3
    Views:
    105
    Brian Candler
    Dec 25, 2008
  5. Dot Baiki
    Replies:
    6
    Views:
    164
    Dot Baiki
    Jan 23, 2011
Loading...

Share This Page