【问题标题】:Reading UTF-8 with BOM in ruby 2.5.0在 ruby​​ 2.5.0 中使用 BOM 读取 UTF-8
【发布时间】:2018-07-30 03:17:40
【问题描述】:

有没有办法在 Ruby v2.5.0 上读取以 UTF-8 编码并带有 BOM(字节顺序标记)的文件?

在 Ruby 2.3.1 上,这曾经可以工作:

 csv = CSV.open(file_path, encoding: 'bom|utf-8')

但是,在 2.5.0 上出现以下错误:

  ArgumentError:
    unknown encoding name - bom|utf-8 

【问题讨论】:

标签: ruby csv encoding utf-8 utf


【解决方案1】:

你也可以试试这个:

File.open(file_path, "r:bom|utf-8")

【讨论】:

    【解决方案2】:

    你可以试试这个:

    require 'file_with_bom'
    
    File.open(file_path, "w:utf-8", :bom => true ) do |csv|
    
    end
    

    效果很好

    【讨论】:

      猜你喜欢
      • 2011-06-21
      • 1970-01-01
      • 1970-01-01
      • 2014-03-04
      • 1970-01-01
      • 2020-01-25
      • 2018-10-28
      相关资源
      最近更新 更多