新手问题 RUBY 2.0 UTF-8 转 GBK 错误,怎么解决?

bluexuemei · 2014年11月02日 · 最后由 bluexuemei 回复于 2014年11月03日 · 6532 次阅读

有时候明明是 UTF-8 编码,可是 s.encode('gbk'),转码失败,怎么解决呢? s='5233496214,鐖遍鏅敾,绂忓缓 鑾嗙敯,缁樼敾鐖卞ソ鑰呫€傚井淇″彿锛?35421815,,,f,2085,1999,66,0,2014-07-26 17:51:14,-1,,1894' (utf-8 编码)

name = name.encode('gb2312', 'utf-8')

试试 String#force_encoding()

……csv 的话直接用编辑器打开手动转不行么

#5 楼 @gyorou ,数据很多,手动太。。。。

试试 str.encode('utf-8','gbk',{:invalid => :replace, :undef => :replace, :replace => '?'})

#7 楼 @ylt ,问题解决,谢谢老师,能否解释一下{:invalid => :replace, :undef => :replace, :replace => '?'}的意思

#8 楼 @bluexuemei 能替换的就替换 不能替换的就替换成'?' 我猜是这样

#9 楼 @hging ,大概可能就是这个样子吧

#8 楼 @bluexuemei 具体说明见:http://www.ruby-doc.org/core-1.9.3/String.html :invalid If the value is :replace, encode replaces invalid byte sequences in str with the replacement character. The default is to raise the Encoding::InvalidByteSequenceError exception

:undef If the value is :replace, encode replaces characters which are undefined in the destination encoding with the replacement character. The default is to raise the Encoding::UndefinedConversionError.

:replace Sets the replacement string to the given value. The default replacement string is “uFFFD” for Unicode encoding forms, and “?” otherwise.

:fallback Sets the replacement string by the given object for undefined character. The object should be a Hash, a Proc, a Method, or an object which has [] method. Its key is an undefined character encoded in the source encoding of current transcoder. Its value can be any encoding until it can be converted into the destination encoding of the transcoder.

#11 楼 @ylt ,thank you very much!

需要 登录 后方可回复, 如果你还没有账号请 注册新账号