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


  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
How to read your own YAML file (*****. Yml) in Java
Ruby: CSV :: How to use Table Note
[Ruby] How to convert from lowercase to uppercase and from uppercase to lowercase
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 comment out
Ruby: How to use cookies
[Ruby] How to write blocks
How to convert Java radix
How to request a CSV file as JSON with jMeter
[Ruby] How to split each GraphQL query into a file
[Easy] How to automatically format Ruby erb file with vsCode
[Ruby] How to batch convert strings in an array to numbers
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 install Bootstrap in Ruby
[Ruby] How to use any? Method
How to convert LocalDate and Timestamp
[Beginner] How to delete NO FILE
How to use Ruby inject method
How to execute Ruby irb (interactive ruby)
How to get date data in Ruby
[Java] How to use the File class
How to delete the wrong migration file
[Ruby on Rails] How to use CarrierWave
[Rails] How to convert from erb to haml
Convert numbers to Roman numerals in Ruby
How to delete the migration file NO FILE
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
[Ruby on Rails] How to use kaminari
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
How to find the cause of the Ruby error
[Ruby on Rails] How to display error messages
[Android] How to convert a character string to resourceId
How to add / remove Ruby on Rails columns
How to dump from database (DB) to seeds file
[Ruby] How to use gsub method and sub method
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
[Ruby on Rails] How to install Bootstrap in Rails