0
0

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 1 year has passed since last update.

RubyのCSV出力でUTF8→S-JISに変換する時の文字化け対応

Posted at

UTF8で保存した🐡や、旧漢字といったものをSJISに変換して、そのまま出そうとエラーがでる。
そのため以下のように書けば、SJISに変換する時、文字化け対象は「?」で書きだすことでエラーが出ない。

text = utf8_dummy_data
CSV.open("hoge.csv", "w", encoding: "SJIS") do |csv|
  csv << [text.encode(Encoding::Windows_31J, Encoding::UTF_8, invalid: :replace, undef: :replace)]
end

簡単なことなんだけど、検証とか調査に時間がかかったので記録しておきます。

よかったらいいね!👍
いただけると嬉しいです✌('ω'✌ )三✌('ω')✌三( ✌'ω')✌

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?