更新时间:2023-11-27 12:27:28
使用 ruby 1.9.2 你可以使用模式 r:bom|utf-8
With ruby 1.9.2 you can use the mode r:bom|utf-8
text_without_bom = nil #define the variable outside the block to keep the data
File.open('file.txt', "r:bom|utf-8"){|file|
text_without_bom = file.read
}
或
text_without_bom = File.read('file.txt', encoding: 'bom|utf-8')
或
text_without_bom = File.read('file.txt', mode: 'r:bom|utf-8')
BOM 在文件中是否可用并不重要.
It doesn't matter, if the BOM is available in the file or not.
您也可以将编码选项与其他命令一起使用:
You may also use the encoding option with other commands:
text_without_bom = File.readlines(@filename, "r:utf-8")
(你得到一个包含所有行的数组).
(You get an array with all lines).
或使用 CSV:
require 'csv'
CSV.open(@filename, 'r:bom|utf-8'){|csv|
csv.each{ |row| p row }
}