当前位置 : 主页 > 编程语言 > ruby >

ruby-on-rails – FasterCSV:读取远程CSV文件

来源:互联网 收集:自由互联 发布时间:2021-06-23
我似乎无法让这个工作.我想从不同的Web服务器中提取CSV文件以读取我的应用程序.这就是我想要的方式: url = 'http://www.testing.com/test.csv'records = FasterCSV.read(url, :headers = true, :header_converte
我似乎无法让这个工作.我想从不同的Web服务器中提取CSV文件以读取我的应用程序.这就是我想要的方式:

url = 'http://www.testing.com/test.csv'
records = FasterCSV.read(url, :headers => true, :header_converters => :symbol)

但这不起作用.我试过谷歌搜索,我想出的就是这个摘录:Practical Ruby Gems

所以,我尝试修改如下:

require 'open-uri'
url = 'http://www.testing.com/test.csv'
csv_url = open(url)
records = FasterCSV.read(csv_url, :headers => true, :header_converters => :symbol)

…而且我得到一个无法将Tempfile转换为String错误(来自FasterCSV gem).

谁能告诉我如何使这项工作?

require 'open-uri'
url = 'http://www.testing.com/test.csv'
open(url) do |f|
  f.each_line do |line|
    FasterCSV.parse(line) do |row|
      # Your code here
    end
  end
end

http://www.ruby-doc.org/core/classes/OpenURI.html
http://fastercsv.rubyforge.org/

网友评论