0

I'm trying to parse the first 5 lines of a remote CSV file. However, when I do, it raises Errno::ENOENT exception, and says:

No such file or directory - [file contents] (with [file contents] being a dump of the CSV contents

Here's my code:

  def preview
    @csv = []
    open('http://example.com/spreadsheet.csv') do |file|
      CSV.foreach(file.read, :headers => true) do |row|
        n += 1
        @csv << row
        if n == 5
          return @csv
        end
      end
    end
  end

The above code is built from what I've seen others use on Stack Overflow, but I can't get it to work.

If I remove the read method from the file, it raises a TypeError exception, saying:

can't convert StringIO into String

Is there something I'm missing?

Asherlc
  • 1,111
  • 2
  • 12
  • 28
  • Maybe I'm not aware of this, but to my knowledge, FasterCSV cannot download the document on the fly and parse it - save it to your hard drive first – Anthony Alberto Aug 04 '12 at 04:40

2 Answers2

0

Foreach expects a filename. Try parse.each

pguardiario
  • 53,827
  • 19
  • 119
  • 159
  • Thanks - though when I tried foreach (which seems more elegant, given that I want to load a file anyway), I get the following error: `No such file or directory - http://example.com/spreadsheet.csv` Is there some better way to write this? – Asherlc Aug 04 '12 at 04:00
  • You could save the url as a file first I suppose. – pguardiario Aug 04 '12 at 04:20
  • I realize this is getting a little off topic, but I'm using Carrier Wave to hold these files in s3. Passing in the carrier wave object didn't work either - is there a different tact i should take? Using heroku, so i can't download the file to my server. – Asherlc Aug 04 '12 at 17:21
  • In that case use parse.each, I don't really see it as less elegant anyway. – pguardiario Aug 05 '12 at 00:54
0

You could manually pass each line to CSV for parsing:

require 'open-uri'
require 'csv'

def preview(file_url)
    @csv = []
    open(file_url).each_with_index do |line, i|
        next if i == 0 #Ignore headers
        @csv << CSV.parse(line)
        if i == 5
            return @csv
        end
    end
end

puts preview('http://www.ferc.gov/docs-filing/eqr/soft-tools/sample-csv/contract.txt')
Justin Ko
  • 46,526
  • 5
  • 91
  • 101