hobbydev
hobbydev

Reputation: 1709

How to read the CSV based on encoding in Ruby on Rails

I used https://github.com/roo-rb/roo to parse the CSV file in Ruby on Rails. Code is following as

data = Roo::CSV.new(obj.public_url, csv_options: {encoding: Encoding::ISO_8859_1})

There is an issue when open the CSV file encoded by UTF-8. (Unknown characters is shown.)

Is there any solution to read the CSV file of any encodings?

Or How can I get the encoding type of CSV file?

Upvotes: 2

Views: 2341

Answers (3)

Getu
Getu

Reputation: 179

I think you're specifying the CSV to be encoded as ISO-8859-1 ({encoding: Encoding::ISO_8859_1}), and if you're trying to open the CSV as UTF-8 it will show weird symbols on those characters not accepted by ISO.

I suggest to try with another options to ask Roo to encode the file as UTF-8:

data = Roo::CSV.new(obj.public_url, csv_options: {encoding: Encoding::UTF_8})

Upvotes: 1

H Dox
H Dox

Reputation: 675

I'm assuming your CSV file is get from an url. If this is the case then you need to use open-uri to stream the file and detect its encoding with the following code:

require 'uri'
en = open('obj.public_url').read.encoding => #<Encoding:UTF-8>

Then, you can use roo to read your CSV file with the correct encoding.

data = Roo::CSV.new(obj.public_url, csv_options: {encoding: en})

Upvotes: 0

Sree
Sree

Reputation: 360

Could you try without the csv_options parameter?

Or try the usual:

File.read('foo.csv').encoding => #<Encoding:UTF-8>

Upvotes: 1

Related Questions