简体   繁体   English

Rails 中 UTF-8 中的无效字节序列

[英]invalid byte sequence in UTF-8 in Rails

I have an HL7 file and the name in the file is caled "müller" and thats why i guess i get the Error:我有一个 HL7 文件,文件中的名称被称为“müller”,这就是为什么我猜我得到错误:

Exiting退出

/usr/local/bundle/gems/simple_hl7-1.0.2/lib/simple_hl7/message.rb:115:in `split': invalid byte sequence in UTF-8 (ArgumentError) from /usr/local/bundle/gems/simple_hl7-1.0.2/lib/simple_hl7/message.rb:115:in `parse' from /app/config/initializers/filewatcher.rb:9:in `block in ' from /app/config/initializers/filewatcher.rb:2:in `foreach' from /app/config/initializers/filewatcher.rb:2:in `' from /usr/local/bundle/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:59:in `load' from /usr/local/bundle/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:59:in `load'... /usr/local/bundle/gems/simple_hl7-1.0.2/lib/simple_hl7/message.rb:115:in `split': UTF-8 (ArgumentError) 中的无效字节序列来自 /usr/local/bundle/gems/simple_hl7- 1.0.2/lib/simple_hl7/message.rb:115:in `parse' from /app/config/initializers/filewatcher.rb:9:in `block in' from /app/config/initializers/filewatcher.rb:2 :in `foreach' from /app/config/initializers/filewatcher.rb:2:in `' from /usr/local/bundle/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb: 59:在来自 /usr/local/bundle/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb 的“加载”中:59:在“加载”中...

this is my code这是我的代码

Dir.foreach('lib/hl7/') do |changes|

  if File.basename(changes, ".HL7").include?("ADT")
    p changes
    hl7_file = File.open("lib/hl7/#{changes.to_s}")
    hl7_read = File.read(hl7_file)
      
    hl7_msg = SimpleHL7::Message.parse(hl7_read)
    p hl7_msg
  end
end

i tried to hl7_read = File.read(hl7_file).encode('utf-8') but that didnt work我试图 hl7_read = File.read(hl7_file).encode('utf-8') 但这没有用

thank you for your help谢谢您的帮助

I think you have to set the encoding for the file to UTF-8 or whatever the file originally was.我认为您必须将文件的编码设置为 UTF-8 或文件最初的任何内容。

Have you read this article?你读过这篇文章吗? https://thoughtbot.com/blog/fight-back-utf-8-invalid-byte-sequences https://thoughtbot.com/blog/fight-back-utf-8-invalid-byte-sequences

Btw you can open a file with specific encoding this way顺便说一句,您可以通过这种方式打开具有特定编码的文件

File.open("lib/hl7/#{changes.to_s}", :encoding => 'UTF-8')

I think you have to find out what the original encoding is for that file.我认为您必须找出该文件的原始编码是什么。 Maybe you should be sure that on you machine and server everything is UTF-8 per default and if something is not in UTF-8 you have to convert it first.也许您应该确保在您的机器和服务器上,默认情况下所有内容都是 UTF-8,如果 UTF-8 中没有某些内容,则必须先转换它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM