Ruby 如何处理UTF-8 ArgumentError中的无效字节序列?
我试图运行下面的代码,使用nokogiri解析xml文件。我想从文本中删除新行字符 包含在标签之间。我这里的代码过去是有效的,但由于某种原因,现在不行了。可能是因为我 升级到ruby-1.9.1Ruby 如何处理UTF-8 ArgumentError中的无效字节序列?,ruby,nokogiri,Ruby,Nokogiri,我试图运行下面的代码,使用nokogiri解析xml文件。我想从文本中删除新行字符 包含在标签之间。我这里的代码过去是有效的,但由于某种原因,现在不行了。可能是因为我 升级到ruby-1.9.1 titles = node.search('b') titles.each do |e| unless e.parent.name == "h4" if e.children.children.first.nil? == false puts e.children.childre
titles = node.search('b')
titles.each do |e|
unless e.parent.name == "h4"
if e.children.children.first.nil? == false
puts e.children.children.first.text.gsub("\n","")
end
end
end
运行代码时,出现以下错误:
HI. You're using libxml2 version 2.6.16 which is over 4 years old and has
plenty of bugs. We suggest that for maximum HTML/XML parsing pleasure, you
upgrade your version of libxml2 and re-install nokogiri. If you like using
libxml2 version 2.6.16, but don't like this warning, please define the constant
I_KNOW_I_AM_USING_AN_OLD_AND_BUGGY_VERSION_OF_LIBXML2 before requring nokogiri.
test.rb:35:in `gsub': invalid byte sequence in UTF-8 (ArgumentError)
您可以尝试通过RVM安装1.9.2
curl -L https://get.rvm.io | bash
rvm install 1.9.2
如果希望ruby默认安装为rvm 1.9.2,那么
rvm use 1.9.2 --default
注:以上等同于:
curl -L https://get.rvm.io | bash -s -- --ruby=1.9.2
你愿意不按照建议升级吗?你真的应该按照建议(升级,而不是隐藏警告!)然后再次测试你的代码。如果错误仍然存在,请给出注释。顺便说一下,您还应该使用Ruby 1.9.2,而不是1.9.1!在哪里可以找到升级Mac OSX中ruby的说明?据我所知,我只能通过RVMIt来实现这一点,但使用MacOSX的Ruby可能不是一个好主意。我会选择MacPorts/Fink或者自制软件。