Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby 1.9.2 - Read and parse a remote CSV

I am looking for a way to read and parse locally a remote CSV (hosted on a particular website).

I found on the Internet a couple of interesting examples that make use of FasterCSV, that in ruby 1.9.2 has been merged into CSV. I found that you can read a remote CSV using the gems 'csv' and 'open-uri' this way:

require 'csv'
require 'open-uri'

def read(url)
  open(url) do |f|
    f.each_line do |l|
      CSV.parse(l) do |row|
        puts row
      end
    end
  end
end

But when I call this function, I get an exception:

ERROR IOError: closed stream

Anyone can explain me why? Is there anything wrong? Should I choose another approach for reading remote CSV's?

Update

The best solution I've found till now is this:

def read(url)
  data = []
  begin
    open(url) do |f|
      data = CSV.parse f
    end
  rescue IOError => e
    # Silently catch the exception ...
  end

  return data
end

but it somewhat seems not so clean. I really do not like silently catching an exception where it shouldn't be ...

Update 2

I can reproduce the error using both

ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.4.0]

and

ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.7.0]

This is the code from my test.rb file:

require 'rubygems'
require 'open-uri'
require 'csv'

def read(url)
  data = []
  begin
    open(url) do |f|
      data = CSV.parse f
    end
  end

  puts data
end

read("http://www.euribor-ebf.eu/assets/modules/rateisblue/processed_files/myav_EURIBOR_2011.csv")

And this is the output of the ruby test.rb command

/Users/marzu/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/open-uri.rb:152:in `close': closed stream (IOError)
from /Users/marzu/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/open-uri.rb:152:in `open_uri'
from /Users/marzu/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/open-uri.rb:671:in `open'
from /Users/marzu/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/open-uri.rb:33:in `open'
from test.rb:8:in `read'
from test.rb:16:in `<main>'

I am using rvm 1.6.9 on Mac OS X 10.6.7.

Any suggestions?

like image 591
marzapower Avatar asked May 14 '11 22:05

marzapower


1 Answers

On Mac OS X 10.6.7, using ruby r1.9.2, I get the same error as displayed above. But using the following code to read CSV files works for the example URL provided:

require 'rubygems'
require 'open-uri'
require 'csv'

def read(url)
 CSV.new(open(url), :headers => :first_row).each do |line|
   puts line
   puts line[0]
   puts line['FEB11']
 end
end

read("http://www.euribor-ebf.eu/assets/modules/rateisblue/processed_files/myav_EURIBOR_2011.csv")
like image 153
Steve Wilhelm Avatar answered Sep 29 '22 06:09

Steve Wilhelm