Reading files with comments with FasterCSV

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs