3

私はこれを機能させることができないようです。別の 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)

...そしてcan't convert Tempfile into Stringエラーが発生します(FasterCSV gemから来ています)。

誰かがこれを機能させる方法を教えてもらえますか?

4

5 に答える 5

5
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/

于 2009-01-12T22:53:51.327 に答える
1

Net::HTTPたとえば、ファイルを取得し、それをフィードしますFasterCSV

から抽出ri Net::HTTP

 require 'net/http'
 require 'uri'

 url = URI.parse('http://www.example.com/index.html')
 res = Net::HTTP.start(url.host, url.port) {|http|
   http.get('/index.html')
 }
 puts res.body
于 2009-01-12T14:52:37.570 に答える
1

小さなタイプミスがありました。FasterCSV.parse代わりに使用する必要がありましたFasterCSV.read

data = open('http://www.testing.com/test.csv')
records = FasterCSV.parse(data)
于 2010-03-02T22:33:33.357 に答える
0

ペーパークリップでCSVファイルをアップロードしてCloudfilesに保存し、Delayed_jobでファイル処理を開始します。

これは私のために働いた:

require 'open-uri'
url = 'http://www.testing.com/test.csv'
open(url) do |file|
  FasterCSV.parse(file.read) do |row|
    # Your code here
  end
end
于 2010-10-10T18:48:40.137 に答える
0

私はrioでダウンロードします-次のように簡単です:

require 'rio'
require 'fastercsv'

array_of_arrays = FasterCSV.parse(rio('http://www.example.com/index.html').read)
于 2009-08-10T22:30:42.910 に答える