I want to read a CSV file produced by an export from one of my controllers. Here is the simple code:
def export_houses
houses_file = open("http://127.0.0.1:3000/houses/export.csv")
houses = CSV.open(houses_file, 'r:bom|utf-8', { headers: true })
...
end
The problem occurs on the CSV.open
line, where I get the following error message:
TypeError: no implicit conversion of StringIO into String
from /Users/htaidirt/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/csv.rb:1256:in `initialize'
houses_file
is correctly found. It's a StringIO
class, but I wanted a File
class to use with CSV.open
.
Do you have an idea how to proceed? Thanks.
You can read a csv file from http server like this :
require 'open-uri'
require 'csv'
url = 'http://127.0.0.1:3000/houses/export.csv'
houses = CSV.new(open(url), :headers => :first_row)
or you can parse it with parse method
require 'open-uri'
require 'csv'
url = 'http://127.0.0.1:3000/houses/export.csv'
houses = CSV.parse(open(url).read, :headers => true)
Hope this helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With