Ruby/Rails CSV parsing, invalid byte sequence in UTF-8

You can supply source encoding straight in the file mode parameter:

CSV.foreach( "file.csv", "r:windows-1250" ) do |row|
   <your code>
end

You need to tell Ruby that the file is in ISO-8859-1. Change your file open line to this:

file=File.open("input_file", "r:ISO-8859-1")

The second argument tells Ruby to open read only with the encoding ISO-8859-1.


Specify the encoding with encoding option:

CSV.foreach(file.path, headers: true, encoding:'iso-8859-1:utf-8') do |row|
  ...
end