Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 这段代码能够从文件系统中读取CSV文件,但是从URL读取呢?_Ruby On Rails_Ruby_Csv_Rake_Fastercsv - Fatal编程技术网

Ruby on rails 这段代码能够从文件系统中读取CSV文件,但是从URL读取呢?

Ruby on rails 这段代码能够从文件系统中读取CSV文件,但是从URL读取呢?,ruby-on-rails,ruby,csv,rake,fastercsv,Ruby On Rails,Ruby,Csv,Rake,Fastercsv,我编写了这个rake任务,它使我能够从我的应用程序的本地文件系统上的文件中读取csv文件,但是我如何调整它以使它从url读取文件呢 desc "This class will read a csv file and display its contents on the screen" task :read_csv => :environment do |t, args| require "csv" csv_text = File.read('someFile.csv') cs

我编写了这个rake任务,它使我能够从我的应用程序的本地文件系统上的文件中读取csv文件,但是我如何调整它以使它从url读取文件呢

desc "This class will read a csv file and display its contents on the screen"

 task :read_csv => :environment do |t, args|
 require "csv"

 csv_text = File.read('someFile.csv')
 csv = CSV.parse(csv_text, :headers=>true)
 csv.each do |row|
   puts row
 end
end
如果有人可以帮助我的代码或一些当前的链接,将不胜感激。我找到的大多数链接都是针对以前版本的rails,而FasterCSV不是ruby的一部分


谢谢

这个问题与
FasterCSV
没有什么关系,因为您正在将整个文件加载到一个字符串中。那么问题就变成了,

使用NET::HTTP怎么样

desc "This class will read a csv file from url and display its contents on the screen"

  task :read_csv => :environment do |t, args|
  require "csv"
  require 'net/http'

  uri = URI('http://www.xxx.ccc.xxx.ca/~xxx/xxx.csv')
  csv_text = Net::HTTP.get(uri)
  csv = CSV.parse(csv_text, :headers=>true)
  csv.each do |row|
    puts row
  end
end

这只是一个小小的调整,只从url获取,不使用https,但你有这个想法,对吗?:)

打开uri读取远程文件的帮助

require 'csv'
require 'open-uri'

csv_text = open('http://www.vvv.hh.yyy.ggg/~hhhh/uuuu.csv')
csv = CSV.parse(csv_text, :headers=>true)
csv.each do |row|
  puts row
end

如果我想要https呢?你有没有关于这方面的参考/链接?谢谢,如果您想使用HTTPS,请查看此页面并向下滚动到HTTPS部分:当获取CSV需要身份验证头和发布的JSON数据时,有没有关于如何进行相同调用的指导?