NET :: http.get_response.bodyのラテン語(キリル)シンボルを備えたASCII-8ビットのROR ASCII-8ビット
-
13-12-2019 - |
質問
Net :: HTTP経由でデータを取得する必要がある場合は、ASCII-8ビットでの応答を受け取ることで適しています。問題はこれをUTF8にエンコードし、すべてのラテンシンボルを保存する方法ですか?
@content.encode('utf-8', 'binary', :invalid => :replace,
:undef => :replace, :replace => '')
では、すべてのキリルシンボルを緩めます
@content.encode('utf-8', 'binary')
を使用して"\xCB" from ASCII-8BIT to UTF-8
エラー
@content.force_encoding("UTF-8)
では、キリルシンボルの代わりに↓が取得されます
Google検索で答えが見つかりません。
解決
問題は
で解決されていますbegin
cleaned = response.body.dup.force_encoding('UTF-8')
unless cleaned.valid_encoding?
cleaned = response.body.encode( 'UTF-8', 'Windows-1251' )
end
content = cleaned
rescue EncodingError
content.encode!( 'UTF-8', invalid: :replace, undef: :replace )
end
.
です。 所属していません StackOverflow