rb:bom|utf-8 在 Ruby 中的 CSV.open 中意味着什么?

Hen*_*ang 3 ruby csv byte-order-mark

中的意思是什么'rb:bom|utf-8'

CSV.open(csv_name, 'rb:bom|utf-8', headers: true, return_headers: true) do |csv|

我可以这么理解:

  1. r意思是读
  2. bom是一种\xEF\xBB\xBF在文件开头指示字节顺序的文件格式。
  3. utf-8是一种文件格式

但:

  1. 我不知道它们是如何组合在一起的,也不知道为什么需要写所有这些来读取 csv
  2. 我正在努力寻找这方面的文档。https://ruby-doc.org/stdlib-2.6.1/libdoc/csv/rdoc/CSV.html中似乎没有记录

更新:

找到了一个非常有用的文档: https ://ruby-doc.org/core-2.6.3/IO.html#method-c-new-label-Open+Mode

Mec*_*cki 6

(接受的答案不是不正确而是不完整)

rb:bom|utf-8转换为人类可读的句子意味着:

打开文件r以二进制模式 ( ) 进行读取 ( b),并查找 Unicode BOM 标记 ( bom) 以检测编码,或者,如果未找到 BOM 标记,则假定为 UTF-8 编码 ( utf-8)。

A BOM marker can be used to detect if a file is UTF-8 or UTF-16 and in case it is UTF-16, whether that is little or big endian UTF-16. There is also a BOM marker for UTF-32, yet Ruby doesn't support UTF-32 as of today. A BOM marker is just a special reserved byte sequence in the Unicode standard that is only used for the purpose of detecting the encoding of a file and it must be the first "character" of that file. It's recommended and typically used for UTF-16 as it exists in two different variants, it's optional for UTF-8 and usually if a file is Unicode but has no BOM marker, it is assumed to be UTF-8.