Ruby 1.9.2 - 读取和解析远程CSV [英] Ruby 1.9.2 - Read and parse a remote CSV
问题描述
我正在寻找一种方法来本地读取和解析远程CSV(托管在特定网站上)。
我在互联网上发现了几个有趣的例子使用FasterCSV,在ruby 1.9.2已被合并到CSV。我发现你可以使用gems'csv'和'open-uri'以这种方式读取远程CSV:
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
但是当我调用这个函数时,我得到一个异常:
错误IOError:closed stream
任何人都可以解释为什么?有什么不对吗?
更新
到目前为止我找到的最佳解决方案是这样的:
def read(url)
data = []
begin
open (url)do | f |
data = CSV.parse f
end
rescue IOError => e
#静默捕获异常...
end
返回数据
end
但它有点似乎不那么干净。我真的不喜欢默默地捕捉一个异常,不应该是...
更新2
我可以使用这两个重现错误。
ruby 1.9.2p0(2010-08-18 revision 29036)[x86_64-darwin10.4.0 ]
和
ruby 1.9.2p180(2011-02-18 revision 30909)[x86_64-darwin10.7.0]
这是我的 test.rb
文件的代码:
require'rubygems'
require'open-uri'
require'csv'
def read(url)
data = []
begin
open(url)do | f |
data = CSV.parse f
end
end
输入数据
end
read(http:// www.euribor-ebf.eu/assets/modules/rateisblue/processed_files/myav_EURIBOR_2011.csv)
这是 ruby test.rb
命令的输出
用户/ marzu / .rvm / rubies / ruby-1.9.2-p180 / lib / ruby / 1.9.1 / open-uri.rb:152:在`close':closed stream(IOError)
从/ Users /marzu/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/open-uri.rb:152:in`open_uri'
来自/Users/marzu/.rvm/rubies /ruby-1.9.2-p180/lib/ruby/1.9.1/open-uri.rb:671:in`open'
从/Users/marzu/.rvm/rubies/ruby-1.9.2- p180 / lib / ruby / 1.9.1 / open-uri.rb:33:在'open'
从test.rb:8:在`read'
从test.rb: < main>'
我使用 rvm 1.6.9 $
在Mac OS X 10.6.7上,使用ruby r1.9.2,我得到与上面显示的相同的错误。但是使用以下代码来读取CSV文件适用于提供的示例URL:
require'rubygems'
require' open-uri'
require'csv'
def read(url)
CSV.new(open(url),:headers =>:first_row)。线|
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)
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?
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")
这篇关于Ruby 1.9.2 - 读取和解析远程CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!