# Newbie: fastcsv: Read rows, print rows

**URL:** <https://www.ruby-forum.com/t/newbie-fastcsv-read-rows-print-rows/108181>\
**Category:** Ruby\
**Created:** [June 29, 2007, 8:51pm UTC](https://www.ruby-forum.com/t/newbie-fastcsv-read-rows-print-rows/108181 "2007-06-29T20:51:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 29, 2007, 8:51pm UTC](https://www.ruby-forum.com/t/newbie-fastcsv-read-rows-print-rows/108181/1 "2007-06-29T20:51:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 29, 2007, 9:08pm UTC](https://www.ruby-forum.com/t/newbie-fastcsv-read-rows-print-rows/108181/2 "2007-06-29T21:08:11Z")

</div>

On Jun 29, 2007, at 1:50 PM, removed\_email\_address@domain.invalid 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/](http://fastercsv.rubyforge.org/)

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

[http://fastercsv.rubyforge.org/classes/FasterCSV.html](http://fastercsv.rubyforge.org/classes/FasterCSV.html)

There are also some examples in the source:

[http://viewvc.rubyforge.mmmultiworks.com/cgi/viewvc.cgi/trunk/](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 G. II

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 29, 2007, 10:21pm UTC](https://www.ruby-forum.com/t/newbie-fastcsv-read-rows-print-rows/108181/3 "2007-06-29T22:21:21Z")

</div>

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!

`
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 G. II removed_email_address@domain.invalid`

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 2, 2007, 6:56pm UTC](https://www.ruby-forum.com/t/newbie-fastcsv-read-rows-print-rows/108181/4 "2007-07-02T18:56:39Z")

</div>

removed\_email\_address@domain.invalid wrote:

> `
> 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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 17, 2007, 1:26am UTC](https://www.ruby-forum.com/t/newbie-fastcsv-read-rows-print-rows/108181/5 "2007-07-17T01:26:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 17, 2007, 5:10am UTC](https://www.ruby-forum.com/t/newbie-fastcsv-read-rows-print-rows/108181/6 "2007-07-17T05:10:48Z")

</div>

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

> 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 G. II
