Is there a way to read files encoded in UTF-8 with BOM (Byte order marks) on Ruby v2.5.0?
On Ruby 2.3.1 this used to work:
csv = CSV.open(file_path, encoding: 'bom|utf-8')
However, on 2.5.0 the following error ocurrs:
ArgumentError:
unknown encoding name - bom|utf-8
The Unicode Standard permits the BOM in UTF-8, but does not require or recommend its use. Byte order has no meaning in UTF-8, so its only use in UTF-8 is to signal at the start that the text stream is encoded in UTF-8, or that it was converted to UTF-8 from a stream that contained an optional BOM.
To Add BOM to a UTF-8 file, we can directly write Unicode \ufeff or three bytes 0xEF , 0xBB , 0xBF at the beginning of the UTF-8 file. The Unicode \ufeff represents 0xEF , 0xBB , 0xBF , read this. 1.1 The below example, write a BOM to a UTF-8 file /home/mkyong/file. txt .
1 Answer. Select “Save As” from File menu, go to Save button and open its dropdown menu, select “Save with Encoding…”, choose “Unicode (UTF-8 without signature)”.
You can try this as well:
File.open(file_path, "r:bom|utf-8")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With