ruby / csv

CSV Reading and Writing
https://ruby.github.io/csv/
BSD 2-Clause "Simplified" License
178 stars 113 forks source link

CSV::Table#headers returns [:"", :""] when header is Japanese #237

Closed ghost closed 2 years ago

ghost commented 2 years ago

Steps to reproduce

# frozen_string_literal: true

require 'csv'

header = %w[都道府県 桃の生産量]
rows = [['山梨県', '37,600トン'], ['福島県', '22,500トン'], ['長野県', '12,100トン']]

CSV.open('momo.csv', 'w') do |csv|
  csv << header
  rows.map { |row| csv << row }
end

csv_table = CSV.table('momo.csv')

pp csv_table.headers

Expected behavior

csv_table#headers returns [:都道府県, :桃の生産量]

Actual behavior

csv_table#headers returns [:"", :""]

System configuration

Ruby version: ruby 3.1.0p0 csv version: csv (default: 3.2.2)

kou commented 2 years ago

I understand your expected behavior but we can't change the current behavior to keep compatibility.

Could you specify header_converters: explicitly?

csv_table = CSV.table('momo.csv', header_converters: lambda {|h| h.to_sym})
kou commented 2 years ago

We can provide :symbol_raw default header converter by adding a converter to CSV::HeaderConverters. If we provide :symbol_raw, we can write like the following:

csv_table = CSV.table('momo.csv', header_converters: :symbol_raw)

@ericgpks Do you want to work on this?

ericgpks commented 2 years ago

@kou I would like to try to resolve this one! Thanks a lot!! Can you assign this on to me?

kou commented 2 years ago

@ericgpks Sure. Go ahead!

kou commented 2 years ago

Done by #239.

ghost commented 2 years ago

Thank you!!