Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import CSV from url Errno::ENAMETOOLONG: file name too long

I'm trying to import a CSV file from a url but i'm getting Errno::ENAMETOOLONG: file name too long. I process the file as follow:

require 'open-uri'
url = "http://de65.grepolis.com/data/csv.txt"
url_data = open(url).read()

SmarterCSV.process(url_data, {
  ...
})

What am i missing ?

like image 645
Mini John Avatar asked May 06 '14 18:05

Mini John


2 Answers

You have to pass a filename which should be on server. rightnow you are passing all data . Do something like this

require 'open-uri'
url = "http://de65.grepolis.com/data/csv.txt"
url_data = open(url).read()
File.open('/tmp/file_name', 'w') { |file| file.write(url_data) }
SmarterCSV.process('/tmp/file_name',{ })
like image 93
Paritosh Piplewar Avatar answered Nov 26 '22 04:11

Paritosh Piplewar


I had the same problem using the standard CSV library to pull in a CSV file via an http url. I was able to solve the issue without needing to write to a temporary server file with code like this:

require 'open-uri'
require 'csv'
url = "http://de65.grepolis.com/data/csv.txt"
url_data = open(url).read()
CSV.parse(url_data, headers: true).each do |row|
  # per row processing code ...
end
like image 37
QuarkleMotion Avatar answered Nov 26 '22 03:11

QuarkleMotion