简体   繁体   English

无效的多字节字符 (UTF-8) 错误,Ruby

[英]invalid multibyte char (UTF-8) Error, Ruby

I tried to run an ruby script in rails with the command rails runner.我尝试使用命令 rails runner 在 rails 中运行 ruby 脚本。 The ruby file, looks something like this and should create new patients: ruby 文件看起来像这样,应该会创建新患者:

 Patient.create!({:vorname => 'Josepha', :nachnahme => 'Brecht', :geburtsdatum => '25.04.1963', :strasse => 'Umdorf', :ort => 'Wörthss', :plz => '93093'})
 Patient.create!({:vorname => 'Tumba', :nachnahme => 'Hoch', :geburtsdatum => '17.77.1956', :strasse => 'Hamaß 1', :ort => 'Brenn', :plz => '93189'})

But somehow my code has problems with the german language!但不知何故,我的代码与德语有问题! Im programming beginner and do not know what i have to change!我是编程初学者,不知道我必须改变什么! Thanks for help!感谢帮助!

 C:\Sites\what>rails runner patienten.rb
 C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/c
 ommands/runner.rb:51:in `eval': patienten.rb:2: invalid multibyte char (UTF-8) (
 SyntaxError)
 patienten.rb:2: syntax error, unexpected tIDENTIFIER, expecting '}'
 ...> 'Schlossberg', :ort => 'Wörth', :plz => '93086'})
 ...                               ^
 patienten.rb:2: syntax error, unexpected tINTEGER, expecting $end
 ...:ort => 'Wörth', :plz => '93086'})
 ...                               ^
    from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.1
 3/lib/rails/commands/runner.rb:51:in `<top (required)>'
    from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.1
 3/lib/rails/commands.rb:64:in `require'
    from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.1
 3/lib/rails/commands.rb:64:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

What format is this file in? 该文件的格式是什么? Are you sure it's UTF-8 and not Windows 1252 as is the default in Windows? 您确定它是UTF-8而不是Windows 1252,因为它是Windows中的默认设置吗?

In Ruby 1.9, the header in your file needs to indicate the actual formatting used: 在Ruby 1.9中,文件中的标头需要指明所使用的实际格式:

# encoding: UTF-8

If that doesn't work, you may need to experiment with others: 如果这不起作用,则可能需要与其他人一起尝试:

# encoding: Windows-1252

Another common format is ISO Latin1 : 另一种常见格式是ISO Latin1

# encoding: ISO-8859-1

Both 1252 and 8859-1 are single-byte character sets, each character is always one byte, where UTF-8 is variable length, each character is one or more bytes. 1252和8859-1均为单字节字符集,每个字符始终为一个字节,其中UTF-8为可变长度,每个字符为一个或多个字节。

If you need to convert between formats, usually you can open in an editor that's encoding aware and "Save As..." with the encoding you want. 如果需要在格式之间进行转换,通常可以在一个支持编码的编辑器中打开,并使用所需的编码“另存为...”。 Otherwise you might try using iconv to convert it for you. 否则,您可以尝试使用iconv为您进行转换。

Add

# -*- encoding : utf-8 -*-

at the top of the file 在文件的顶部

put these two line at the top of the script. 将这两行放在脚本的顶部。

#!/bin/env ruby
# encoding: utf-8
irb(main):088:0> "hi\x99!".encode("UTF-8", "Windows-1252")
=> "hi™!"

Justin Weiss has a great article about encoding in Ruby. Justin Weiss 有一篇关于 Ruby 编码的精彩文章。 https://www.justinweiss.com/articles/3-steps-to-fix-encoding-problems-in-ruby/ https://www.justinweiss.com/articles/3-steps-to-fix-encoding-problems-in-ruby/

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

相关问题 Ruby 1.9的无效多字节字符,设置了编码:utf-8无法修复它 - Invalid multibyte char with Ruby 1.9, setting encoding: utf-8 does not fix it 狮子上的无效多字节字符(US-ASCII)(SyntaxError),#编码:utf-8不起作用 - invalid multibyte char (US-ASCII) (SyntaxError) on lion, # encoding: utf-8 does not work Rails 2.3和ruby 1.9中的无效字节序列utf-8错误 - invalid byte sequence utf-8 error in rails 2.3 and ruby 1.9 使用Rails和Ruby 1.9的无效多字节字符(US-ASCII) - invalid multibyte char (US-ASCII) with Rails and Ruby 1.9 Ruby on Rails在UTF-8中的无效字节序列 - Ruby on Rails invalid byte sequence in UTF-8 错误:将Ruby 1.8.7升级到Ruby 1.9.2后,UTF-8中的字节序列无效 - error: invalid byte sequence in UTF-8 after upgrading ruby 1.8.7 to ruby 1.9.2 在Rails枚举中使用希腊字符时出现“无效的多字节字符”错误 - “invalid multibyte char” error when using greek characters in Rails enum 从Android上载图片时,在Ruby服务器上,utf-8错误中的argumentserror字节序列无效 - argumenterror invalid byte sequence in utf-8 error on Ruby server when uploading images from Android UTF-8编码错误Ruby on Rails - UTF-8 Encoding Error Ruby on Rails “无效的多字节字符(US-ASCII)”使用Ruby on Rails使用正则表达式验证新用户 - “invalid multibyte char (US-ASCII)” validating a new user with regex using Ruby on Rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM