業界トップクラスの求人数を誇る転職エージェントPR

リクルートグループのコネクションを活かした非公開求人も充実、他にはない好条件の求人と出会える

3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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")

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

3
3
0

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

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?