Ruby1.9读取txt乱码问题
出以下错误
in`split':invalidbytesequenceinGBK(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
end
end
puts "读取文件"
f=File.open("boss2.txt", 'r')
i=0
total=0
f.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)
end
puts "total:#{total}" 相关推荐
似乎已不再年轻 2020-01-11
Justypc 2013-03-29
amberom 2019-12-24
1. 首先,我得知道这个文件具体采用的是什么样的编码,如果是ISO-8859我将会把它转成UTF-8,其他的ASCII这些都先不做处理,这里可以使用file filename或者是enca iconv
守正出奇 2013-08-15
wenteryan 2015-01-28
Charliewolf 2019-11-02
赵丹icons逐LJ 2012-01-09
赵丹icons逐LJ 2019-03-29
gaedmihx 2019-04-17
守正出奇 2019-03-22
守正出奇 2014-12-03
wenteryan 2013-01-05
alwayshelloworld 2012-02-26
赵丹icons逐LJ 2011-10-31
守正出奇 2011-07-20
Pig00 2011-07-11
gotowqj 2011-07-07
宁静致远 2011-06-28