Ruby 1.9.2 - Read and Parse a Remote CSV

Ruby 1.9.2 - Read and parse a remote CSV

On Mac OS X 10.6.7, using ruby r1.9.2, I get the same error as displayed above. But using the following code to read CSV files works for the example URL provided:

require 'rubygems'
require 'open-uri'
require 'csv'

def read(url)
CSV.new(open(url), :headers => :first_row).each do |line|
puts line
puts line[0]
puts line['FEB11']
end
end

read("http://www.euribor-ebf.eu/assets/modules/rateisblue/processed_files/myav_EURIBOR_2011.csv")

Reading in CSV files smaller than 10K from S3 with Ruby 1.9.2 p290

I'm going to guess that CSV.read is being handed a StringIO when it wants a String. If so, then you should be able to stick a read call in and switch to CSV.parse to make everyone happy:

lines = CSV.parse(open(resource.csv(:original)).read)

FasterCSV: Read Remote CSV Files

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/

Reliably parse unpredictable CSV formats

The Python CSV package is pretty good at this. However, when dealing with unpredictable CSV formats, I expect you'll have to do maintenance no matter what library you pick.

ruby read a file and select best results

Does this example help:

x = <<HERE
Mary,5
John,23355676
William,432200
Jessica,21
HERE

x.split("\n").sort{|a,b| a.split(',')[1].to_i <=> b.split(',')[1].to_i}[0..2]

# => ["Mary 5", "Jessica 21", "William 432200"]

If you want to reverse the sort then change it to this:

x.split("\n").sort{|a,b| b.split(',')[1].to_i <=> a.split(',')[1].to_i}[0..2]

CSV.parse error undefined method `pos' for #ActionDispatch::Http::UploadedFile:0x000001036cb6b0

I had to do this in Rails 3:

data = params[:dump][:file].read
CSV.parse(data)

params[:dump][:file] is an ActionDispatch object and can't be parsed directly by CSV.parse.

Environment dependent CSV parse issue- Rails MacOSX vs Heroku?

in Ruby 1.8.x, CSV is a completely different library than Ruby 1.9.2.

In Ruby 1.9.2, the CSV library is the Ruby 1.8 FasterCSV library.

Getting Errno::ENOENT: No such file or directory @ rb_sysopen while reading the CSV file from the AWS S3

Try this

require 'open-uri'
require 'csv'

def import(file)
CSV.new(open(file), :headers => :true).each do |row| #First open the file using open
row_hash = row.to_hash.values
data = row_hash[0].split("\t")
.
.
.
end

For more info you can refer this link



Related Topics



Leave a reply



Submit