0
0

こちらのプログラムをリートンで変換しました。
Python3: ヘッダーを考慮した csv ファイルの読み込み

プログラム

csv_header.rb
#! /usr/bin/ruby
#
#	csv_header.rb
#
#						 Jun/23/2024
# ------------------------------------------------------------------
require 'csv'
require 'json'
require 'fileutils'

# ------------------------------------------------------------------
def file_write_proc(file_name, str_out)
	File.write(file_name, str_out)
end

# ------------------------------------------------------------------
file_csv = ARGV[0]
file_json = ARGV[1]

rows = CSV.parse(File.read(file_csv), headers: true).map(&:to_h)
out_str = JSON.dump(rows)
file_write_proc(file_json, out_str)

# ------------------------------------------------------------------

実行

./csv_header.rb cities_header.csv cities.json
./csv_header.rb cities_ex02.csv cities_ex02.json

確認したバージョン

$ ruby --version
ruby 3.2.3 (2024-01-18 revision 52bb2ac0a6) [x86_64-linux-gnu]
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0