# Reading files with comments with FasterCSV

**URL:** <https://www.ruby-forum.com/t/reading-files-with-comments-with-fastercsv/104913>\
**Category:** Ruby\
**Created:** [May 23, 2007, 8:55pm UTC](https://www.ruby-forum.com/t/reading-files-with-comments-with-fastercsv/104913 "2007-05-23T20:55:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![swabianeagle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71c47a/32.png) [@swabianeagle](https://www.ruby-forum.com/u/swabianeagle)\
**Post date:** [May 23, 2007, 8:55pm UTC](https://www.ruby-forum.com/t/reading-files-with-comments-with-fastercsv/104913/1 "2007-05-23T20:55:59Z")

</div>

Hi,

I’m trying to read csv files using FasterCSV.  
I have two problems:

1. Some of my files have a few lines of comments before the actual  
header.  
Is there a way to read the whole file at once, for example with  
csv = FasterCSV.read(“filename”, opt)  
and have the comment lines ignored (either by telling FasterCSV to  
ignore the first n lines or giving FasterCSV a string or regular  
expression to match the beginning of the header).

2. Some of my files have no header, but I still would like to read them  
as a FasterCSV::Table, rather than an Array of Arrays for ease of column  
access and auto-conversion.  
How can I do that and is there a way to add a header?

Thanks,  
Armin

---

<div class="post-metadata">

**Author:** ![swabianeagle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71c47a/32.png) [@swabianeagle](https://www.ruby-forum.com/u/swabianeagle)\
**Post date:** [May 23, 2007, 9:04pm UTC](https://www.ruby-forum.com/t/reading-files-with-comments-with-fastercsv/104913/2 "2007-05-23T21:04:00Z")

</div>

On May 23, 2007, at 1:54 PM, Armin A. wrote:

> Hi,

Hello.

> I’m trying to read csv files using FasterCSV.  
> I have two problems:
> 
> 1. Some of my files have a few lines of comments before the actual  
> header.  
> Is there a way to read the whole file at once, for example with  
> csv = FasterCSV.read(“filename”, opt)  
> and have the comment lines ignored (either by telling FasterCSV to  
> ignore the first n lines or giving FasterCSV a string or regular  
> expression to match the beginning of the header).

There’s no way to get FasterCSV to skip the line. I know one other  
person has made this request in the past though, so it may be an  
option we need to look at adding.

In the meantime, just skip them manually and hand FasterCSV the IO  
object:

csv = open(“filename”) do |io|

# code to skip io ahead to the start of the CSV here…

FCSV.new(io, opt).read  
end

> 1. Some of my files have no header, but I still would like to read  
> them as a FasterCSV::Table, rather than an Array of Arrays for ease  
> of column access and auto-conversion.  
> How can I do that and is there a way to add a header?

Yes, FasterCSV will accept an Array of headers to use (:headers =\>  
[:one, :two, …]).

Hope that helps.

James Edward G. II
