Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

The following code snippet works fine for CSV file sizes larger than 10 K.

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

This is reading the the CSV file stored in Amazon S3 using Paperclip gem.

If the file size is smaller than 10 K however, I get the following error:

ActionView::Template::Error (can't convert StringIO into String):

I googled and found the following post:

http://adayinthepit.com/?p=269

So I tried to use the fastercsv gem, when I ran my program again, here is the error that I get:

ActionView::Template::Error (Please switch to Ruby 1.9's standard CSV library. It's FasterCSV plus support for Ruby 1.9's m17n encoding engine.):

Looks like it is a Catch-22. How can I process files smaller than 10 K in ruby 1.9.2 p290?

Please advise.

Thanks.

Bharat

like image 649
Bharat Avatar asked Oct 24 '22 18:10

Bharat


1 Answers

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)
like image 117
mu is too short Avatar answered Oct 27 '22 10:10

mu is too short