ROR ASCII-8 비트가 net :: http.get_response.body의 비 라틴어 (키릴 문자) 기호로 UTF-8에
-
13-12-2019 - |
문제
NET :: HTTP를 통해 일부 데이터를 가져와야합니다. IT는 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