[Ruby] How to convert CSV file to Yaml (Yml)

procedure

  1. Ruby standard library csv and yml with require method .org / ja / master / library / yaml.html) is read.
require 'csv'
require 'yaml'
  1. When you execute CSV.read (path_to_csv,: headers => true), an instance of CSV :: table class will be returned. Extract the elements (instances of CSV :: Row class) one by one with the map method, and hash the elements with the to_h method. Since the return value of the map method is an array, an array with the hash as an element is created. The point is that the key name of the hash is the CSV column name by adding the : header => true option this time.
hash_arr = CSV.read(path_to_csv, :headers => true).map(&:to_h)
=>[{"region_id"=>"1", "country_id"=>"1", "region_name"=>"Hokkaido"}, {"region_id"=>"2", "country_id"=>"1", "region_name"=>"Aomori Prefecture"},...]
  1. Open the file specified by path in write mode, convert the array with the hash of 2 as an element to Yaml, and save it.
File.open(path_to_yaml, 'w') { |f| f.write(hash_arr.to_yaml) }

Postscript: It seems that the same writing can be done below. Thank you for your comment!

IO.write path_to_yaml, hash_arr.to_yaml

P.S As an aside from here, since I was managing a table (country or region) whose value is basically unchanged in the DB, I converted it this time to manage it with ActiveYaml. Even if it is managed by ActiveYaml, it is convenient because you can operate the table like ActiveRecord (class methods such as all and find can be used).

Recommended Posts

[Ruby] How to convert CSV file to Yaml (Yml)
How to convert erb file to haml
Ruby: CSV :: How to use Table Note
Ruby How to convert between uppercase and lowercase
Convert csv file to fixed length record file using enum
How to handle TSV files and CSV files in Ruby
How to convert a file to a byte array in Java
How to use Ruby return
Convert to Ruby Leet string
Ruby: How to use cookies
How to convert Java radix
[Ruby] How to split each GraphQL query into a file
[Easy] How to automatically format Ruby erb file with vsCode
Convert ruby object to JSON format
How to iterate infinitely in Ruby
How to install ruby through rbenv
How to use Ruby on Rails
How to convert LocalDate and Timestamp
[Beginner] How to delete NO FILE
How to use Ruby inject method
[Java] How to use the File class
[Rails] How to convert from erb to haml
How to add jar file in ScalaIDE
[Core ML] How to convert YOLO v3 to Core ML
Ruby length, size, count How to use
How to achieve file download with Feign
[Ruby] How to use slice for beginners
How to convert java.util.Date, java.sql.Date, LocalDate, ZonedDateTime
[Ruby on Rails] How to use redirect_to
[Easy] How to upgrade Ruby and bundler
How to split Spring Boot message file
How to achieve file upload with Feign
Convert JSON to TSV and TSV to JSON with Ruby
How to ZIP a JAVA CSV file and manage it in a Byte array
[Android] How to convert a character string to resourceId
How to dump from database (DB) to seeds file
How to output CSV created by Rails to S3
How to convert a solidity contract to a Java contract class
How to write code that thinks object-oriented Ruby
How to bind to property file in Spring Boot
[Ruby] How to generate a random alphabet string
How to build the simplest blockchain in Ruby
[ruby] How to receive values from standard input?
Rails "How to delete NO FILE migration files"
[Spring Boot] How to refer to the property file
[Spring Batch] Output table data to CSV file
[Rails] How to convert UC time display to Japanese time display
How to implement Pagination in GraphQL (for ruby)
[Ruby basics] How to use the slice method
[Ruby on Rails] How to write enum in Japanese
Convert large XLSX files to CSV with Apache POI
How to jump from Eclipse Java to a SQL file
How to make LINE messaging function made with Ruby
Mastering Kotlin ~ Convert Java File to Kotlin File Road to Graduation ~ Part 2
[Java] How to extract the file name from the path
[Ruby on Rails] How to change the column name
[Ruby] How to use rbenv (version `x.x.x'is not installed)
Converting TSV files to CSV files (with BOM) in Ruby
How to launch another command in a Ruby program
How to download a file (Servlet, HTML, Apache, Tomcat)
Mastering Kotlin ~ Convert Java File to Kotlin File Road to Graduation ~ Part 1