无效的多字节字符 (UTF-8) 错误,Ruby [英] invalid multibyte char (UTF-8) Error, Ruby

查看:84
本文介绍了无效的多字节字符 (UTF-8) 错误,Ruby的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用命令 rails runner 在 rails 中运行 ruby​​ 脚本.ruby 文件看起来像这样,应该会创建新的患者:

I tried to run an ruby script in rails with the command rails runner. The ruby file, looks something like this and should create new patients:

 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>'

推荐答案

这个文件是什么格式的?您确定它是 UTF-8 而不是 Windows 1252 作为 Windows 中的默认设置吗?

What format is this file in? Are you sure it's UTF-8 and not Windows 1252 as is the default in Windows?

在 Ruby 1.9 中,文件中的标题需要指明实际使用的格式:

In Ruby 1.9, the header in your file needs to indicate the actual formatting used:

# encoding: UTF-8

如果这不起作用,您可能需要与其他人一起试验:

If that doesn't work, you may need to experiment with others:

# encoding: Windows-1252

另一种常见格式是ISO Latin1:

# encoding: ISO-8859-1

1252和8859-1都是单字节字符集,每个字符总是一个字节,其中UTF-8是变长的,每个字符是一个或更多个字节.

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.

如果您需要在格式之间进行转换,通常您可以在编码感知的编辑器中打开并保存As..."使用您想要的编码.否则,您可以尝试使用 iconv 为您转换.

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.

这篇关于无效的多字节字符 (UTF-8) 错误,Ruby的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆