rogeliog rogeliog - 5 months ago 121
Ruby Question

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

I am trying to parse a CSV file generated from an Excel spreadsheet.

Here is my code

require 'csv'
file = File.open("input_file")
csv = CSV.parse(file)


But I get this error

ArgumentError: invalid byte sequence in UTF-8


I think the error is because Excel encodes the file into
ISO 8859-1 (Latin-1)
and not in
UTF-8


Can someone help me with a workaround for this issue, please

Thanks in advance.

Answer

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.