[英]Does Ruby auto-detect a file's codepage?
If a save a text file with the following character б
U+0431
, but save it as an ANSI code page file. 如果保存了带有以下字符
б
U+0431
的文本文件,但将其另存为ANSI代码页文件。
Ruby returns ord = 63
. Ruby返回
ord = 63
。 Saving the file with UTF-8 as the codepage returns ord = 208, 177
代码页返回
ord = 208, 177
使用UTF-8保存文件
Should I be specifically telling Ruby to handle the input encoded with a certain code page? 我应该专门告诉Ruby处理使用特定代码页编码的输入吗? If so, how do you do this?
如果是这样,您该怎么做?
Is that in ruby source code or in a file which is read with File.open
? 这是在ruby源代码中还是在用
File.open
读取的文件中? If it's in the ruby source code, you can (in ruby 1.9) add this to the top of the file: 如果在ruby源代码中,则可以(在ruby 1.9中)将其添加到文件顶部:
# encoding: utf-8
Or you could specify most other encodings (like iso-8859-1). 或者,您可以指定大多数其他编码(例如iso-8859-1)。
If you are reading a file with File.open
, you could do something like this: 如果您正在使用
File.open
读取文件, File.open
可以执行以下操作:
File.open("file.txt", "r:utf-8") {|f| ... }
As with the encoding comment, you can pass in different types of encodings here too. 与编码注释一样,您也可以在此处传递不同类型的编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.