LoginSignup
3

More than 5 years have passed since last update.

Rubyでエンコードのエラーの対処

Posted at

Rubyでのエンコードエラー

rubyでエンコードのことでエラーが出たのでその対処法

使うエンコードを指定する場合は

Encoding.default_external = 'UTF-8'

とやってる。

これに関してはいろりろ書き方があると思うから好きなのを使うとよいと思う。

今回であったのが

invalid byte sequence in UTF-8

みたいなやつ

出会った経緯としてはウェブページをスクレイピングしてたらそこにUTF-8以外の文字があってそれを処理しようとしたら出た。

自分の場合はとりあえずエラーさえ出なければよかったので

string.encode("UTF-16BE", "UTF-8",
           invalid: :replace,
           undef: :replace,
           replace: '.').encode("UTF-8")

これでエラーが出てる部分の文字を.に変えたらエラーは出なくなった。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3