UTF-8中的Ruby无效字节序列
我有以下代码,它给我一个无效的字节序列错误指向initialize
的扫描方法。 有想法该怎么解决这个吗? 对于它的价值,当h1标签和关闭>
之间的(.*)
不存在时,不会发生错误。
#!/usr/bin/env ruby class NewsParser def initialize Dir.glob("./**/index.htm") do |file| @file = IO.read file parsed = @file.scan(/(.*?)(.*)/im) self.write(parsed) end end def write output @contents = output open('output.txt', 'a') do |f| f << @contents[0][0]+"\n\n"+@contents[0][1]+"\n\n\n\n" end end end p = NewsParser.new
编辑:这是错误消息:
news_parser.rb:10:in 'scan': invalid byte sequence in UTF-8 (ArgumentError)
@file = IO.read(file).force_encoding("ISO-8859-1").encode("utf-8", replace: nil)
:使用的组合: @file = IO.read(file).force_encoding("ISO-8859-1").encode("utf-8", replace: nil)
和encoding: UTF-8
解决问题。
谢谢!
结合使用: @file = IO.read(file).force_encoding("ISO-8859-1").encode("utf-8", replace: nil)
和#encoding: UTF-8
解决了这个问题。