FasterCSV:读取远程CSV文件 [英] FasterCSV: Read Remote CSV Files

查看:483
本文介绍了FasterCSV:读取远程CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法让这个工作。我想从不同的Web服务器中提取一个CSV文件,以便读入我的应用程序。这就是我想说的:

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

但这不行。我试过谷歌,所有我想出了这个摘录:实用Ruby Gems < a>



所以,我尝试修改它如下:

  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转换为字符串错误(来自FasterCSV gem)。



任何人都可以告诉我如何

p

  require'open-uri'
url = 'http://www.testing.com/test.csv'
open(url)do | f |
f.each_line do | line |
FasterCSV.parse(line)do | row |
#您的代码在这里
end
end
end

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


I can't seem to get this to work. I want to pull a CSV file from a different webserver to read in my application. This is how I'd like to call it:

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

But that doesn't work. I tried Googling, and all I came up with was this excerpt: Practical Ruby Gems

So, I tried modifying it as follows:

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

... and I get a can't convert Tempfile into String error (coming from the FasterCSV gem).

Can anyone tell me how to make this work?

解决方案

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/

这篇关于FasterCSV:读取远程CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆