读书人

ruby 转化ANSI 编为UTF-八

发布时间: 2013-11-08 17:52:32 作者: rapoo

ruby 转化ANSI 编为UTF-8
用到:gem: rchardet19,string.encoding,string.force_encoding


require 'rchardet19' def convert_encode file    string=File.open(file.location,"r+").read #     cd = CharDet.detect(string)    # 获取文件编码   #cd.encoding    if string.encoding != 'UTF-8' && cd.encoding=="utf-8"      string.force_encoding('UTF-8')     elsif string.encoding != 'UTF-8'      string.force_encoding('GBK')     end   File.open(file.location, "w") { |f| f.puts string.encode("UTF-8") }  end

读书人网 >Ruby Rails

热点推荐