2

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?

3 Answers 3

1

Could you try without the csv_options parameter?

Or try the usual:

File.read('foo.csv').encoding => #<Encoding:UTF-8>
Sign up to request clarification or add additional context in comments.

Comments

1

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})

Comments

0

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})

2 Comments

open('obj.public_url').read.encoding always return <Encoding:ASCII-8BIT> for any encoding type.
Could you download the file first and detect its encoding?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.