Detect encoding

2026-03-10 ruby

Here's a sample code to detect encoding of a CSV file in Ruby. It tries several encodings and checks if it can read the file without errors. If it finds a valid encoding, it prints it out; otherwise, it reports an error.

encodings = ['Windows-31J', 'Shift_JIS', 'EUC-JP', 'UTF-8']
encoding_used = nil

encodings.each do |enc|
  begin
    CSV.foreach(csv_file, encoding: "#{enc}:UTF-8") { |row| break }
    encoding_used = enc
    break
  rescue => e
    # 次のエンコーディングを試す
  end
end

if encoding_used.nil?
  puts "error: can not detect encoding"
  exit 1
end

puts "encoding: #{encoding_used}"