Search code examples
ruby-on-railsrubycharacter-encodingrubymine

Ruby encoding issue


I am saving a CSV file from the web. On the web it appears encoded correctly, but when I save it inside a folder in my app I see that the character "µ" is missing and a "�" appears in its place.

The page where I get the data does not have an encoding in the header, anyway I specify the reading encoding in my code (a rake task) which is the following:

open("public/test.csv", "w:UTF-8") do |file|
  open("http://url.CSV", "r:UTF-8") do |row|
    file.write(row.read)
  end
end

Any help would be appreciated, I am using RubyMine by the way.


Solution

  • The default encoding for HTTP is ISO-8859-1. The saved CSV file used that encoding, so when reading it in Ruby, you must set the encoding correctly:

    File.open('filename.csv', 'r:ISO-8859-1') do |f|
      # do something with the contents
    end