Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String#encode not fixing "invalid byte sequence in UTF-8" error

I know there are multiple similar questions about this error, and I've tried many of them without luck. The problem I'm having involves the byte \xA1 and is throwing

ArgumentError: invalid byte sequence in UTF-8

I've tried the following with no success:

"\xA1".encode('UTF-8', :undef => :replace, :invalid => :replace,
    :replace => "").sub('', '')
"\xA1".encode('UTF-8', :undef => :replace, :invalid => :replace,
    :replace => "").force_encoding('UTF-8').sub('', '')
"\xA1".encode('UTF-8', :undef => :replace, :invalid => :replace,
    :replace => "").encode('UTF-8').sub('', '')

Each line throws the error for me. What am I doing wrong?

UPDATE:

The above lines fail only in IRB. However, I modified my application to encode lines of a CVS file using the same String#encode method and arguments, and I get the same error when reading the line from a file (note: it works if you perform the operations on the same string w/o using IO).

bad_line = "col1\tcol2\tbad\xa1"

bad_line.sub('', '') # does NOT fail
puts bad_line # => col1 col2    bad?

tmp = Tempfile.new 'foo' # write the line to a file to emulate real problem
tmp.puts bad_line
tmp.close

tmp2 = Tempfile.new 'bar'

begin
  IO.foreach tmp.path do |line|
    line.encode!('UTF-8', :undef => :replace, :invalid => :replace, :replace => "")
    line.sub('', '') # fail: invalid byte sequence in UTF-8
    tmp2.puts line
  end
  tmp2.close

  # this would fail if the above error didn't halt execution
  CSV.foreach(tmp2.path) do |row|
    puts row.inspect # fail: invalid byte sequence in UTF-8
  end
ensure
  tmp.unlink
  tmp2.close
  tmp2.unlink
end
like image 805
joshm1 Avatar asked Jul 07 '12 13:07

joshm1


People also ask

What do you means by string?

string noun (ROPE) B2 [ C or U ] (a piece of) strong, thin rope made by twisting very thin threads together, used for fastening and tying things: a package tied with string. a ball/piece of string.

What is a string give example?

1. A string is any series of characters that are interpreted literally by a script. For example, "hello world" and "LKJH019283" are both examples of strings. In computer programming, a string is attached to a variable as shown in the example below.

What is string and its function?

String functions are used in computer programming languages to manipulate a string or query information about a string (some do both). Most programming languages that have a string datatype will have some string functions although there may be other low-level ways within each language to handle strings directly.

Why is it called a string?

Strings are called "strings" because they are made up of a sequence, or string, of characters.


2 Answers

It would seem that ruby thinks that the string encoding is already utf8, so when you do

line.encode!('UTF-8', :undef => :replace, :invalid => :replace, :replace => "")

it doesn't actually do anything because the destination encoding is the same as the current encoding (at least that's my interpretation of the code in transcode.c)

The real question here is whether your starting data is valid in some encoding that isn't utf-8 or whether this is data that is supposed to be utf-8 but has a few warts in it that you want to discard.

In the first case, the correct thing to do is tell ruby what this encoding is. You can do this when you open the file

File.open('somefile', 'r:iso-8859-1')

will open the file, interpreting its contents as iso-8859-1

You can even get ruby to transcode for you

File.open('somefile', 'r:iso-8859-1:utf-8')

will open the file as iso-8859-1, but when you read data from it the bytes will be converted to utf-8 for you.

You can also call force_encoding to tell ruby what a string's encoding is (this doesn't modify the bytes at all, it just tells ruby how to interpret them).

In the second case, where you just want to dump whatever nasty stuff has got into your utf-8, you can't just call encode! as you have because that's a no-op. In ruby 2.1 and higher, you can use String#scrub, in previous versions you can do this

line.encode!('UTF-16', :undef => :replace, :invalid => :replace, :replace => "")
line.encode!('UTF-8')

We first convert to utf-16. Since this is a different encoding, ruby will actually replace our invalid sequences. We can then convert back to utf-8. This won't lose us any extra data because utf-8 and utf-16 are just two different ways of encoding the same underlying character set.

like image 103
Frederick Cheung Avatar answered Sep 22 '22 23:09

Frederick Cheung


Maybe you are running this code in IRB. I have had a lot of encoding issues with IRB. In this case, try saving this code as a .rb file and run the code from the command line.

like image 38
Patrick Oscity Avatar answered Sep 24 '22 23:09

Patrick Oscity