Invalid byte sequence in UTF-8 (ArgumentError)

末鹿安然 提交于 2020-01-02 00:14:49

问题


I'm trying to run a Ruby script, and always getting an error on this line:

file_content.gsub(/dr/i,'med')

Where I'm trying to replace "dr" by "med".

The error is:

program.rb:4:in `gsub': invalid byte sequence in UTF-8 (ArgumentError)

Why is that, how can I fix this issue?

I'm working on a MAC OS X Yosemite machine, with Ruby 2.2.1p85.


回答1:


Probably your string is not in UTF-8 format, so use

if ! file_content.valid_encoding?
  s = file_content.encode("UTF-16be", :invalid=>:replace, :replace=>"?").encode('UTF-8')
  s.gsub(/dr/i,'med')
end

See "Ruby 2.0.0 String#Match ArgumentError: invalid byte sequence in UTF-8".



来源:https://stackoverflow.com/questions/29877310/invalid-byte-sequence-in-utf-8-argumenterror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!