读书人

Ruby1.9读取txt乱码有关问题

发布时间: 2012-09-14 11:53:44 作者: rapoo

Ruby1.9读取txt乱码问题
出以下错误
in `split': invalid byte sequence in GBK (ArgumentError)


解决方案:

require "iconv"class String  def to_gbk    Iconv.iconv("GBK//IGNORE", "UTF-8//IGNORE", self).to_s  end  def to_utf8    #p "my own string"    Iconv.iconv("UTF-8//IGNORE", "GBK//IGNORE", self).to_s  end  def to_utf8_valid    if !self.valid_encoding?      ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')      return ic.iconv(self)    end    self  endendputs "读取文件"f=File.open("boss2.txt", 'r')i=0total=0f.each_line do |line|  i+=1  line_arr=line.to_utf8_valid.split(/\t/)  len =line_arr.length  if (len!=41)    puts "line: #{line_arr[0]} #{len}"    total+=1  end  #puts(i,len)endputs "total:#{total}"

读书人网 >Ruby Rails

热点推荐