Sometimes I get this β invalid byte sequence in UTF-8 β error when I read the contents from a file. Note. This only happens when there are special characters in the string. I tried to open the file without "r: UTF-8", but still getting the same error.
open(file, "r:UTF-8").each_line { |line| puts line.strip(",") } # line.strip generates the error
File contents:
# encoding: UTF-8 290919,"SE","26","Skβ°l","",59.4500,17.9500,, # this errors out 290956,"CZ","45","HornΓ Bradlo","",49.8000,15.7500,, # this errors out 290958,"NO","02","Svaland","",58.4000,8.0500,, # this works
This is a CSV file that I received from the outside, and I'm trying to import it into my database, it did not come with "# encoding: UTF-8" at the top, but I added this, since I read somewhere to fix this problem, but it not this way.: (
Environment:
- Rails v3.0.3
- ruby 1.9.2p0 (2010-08-18 version 29036) [x86_64-darwin10.5.0]
ruby ruby-on-rails character-encoding
kapso Jan 15 2018-11-11T00: 00Z
source share