Skip to content

Instantly share code, notes, and snippets.

@rainyjonne
Last active October 5, 2020 02:18
Show Gist options
  • Save rainyjonne/780d50cf9d6967c344650a9a91a72c7f to your computer and use it in GitHub Desktop.
Save rainyjonne/780d50cf9d6967c344650a9a91a72c7f to your computer and use it in GitHub Desktop.
tsv_to_yml.rb 109078513
# frozen_string_literal: true
require 'csv'
require 'yaml'
# check file is tsv or not
def tsv?(filename)
return raise('File not exist!') unless File.exist?(filename.to_s) # No file!
content = File.read(filename.to_s, encoding: 'utf-8')
return raise('Encoding Error!') unless content.valid_encoding? # Wrong encoding!
filename.include?('.tsv')
end
input_tsv_file = ARGV[0] if tsv?(ARGV[0])
output_yaml_file = ARGV[1] if ARGV[1].to_s.include?('.yml')
def tsv2yaml(input_tsv_file, output_yaml_file)
# turn tsv into csv & change it to yaml
csv_table = CSV.table(input_tsv_file, { col_sep: "\t" })
# change key type from symbol to string to avoid test error
headers = csv_table.headers.map(&:to_s)
# Make a array of several hashes(several rows)
row_hashes = csv_table.map do |row|
# first () produce [{"name" => "Ken"}, {"gender" => "Male"}]
# to_s to avoid implicit type error
# merge several hashes into one hash, produce [{"name" => "Ken", "gender" => "Male"}]
(headers.map { |header| { header => row[header.to_sym].to_s } }).reduce({}, :merge)
end
if output_yaml_file then File.open(output_yaml_file, 'w') { |f| f.puts row_hashes.to_yaml }
else puts row_hashes.to_yaml end
end
tsv2yaml(input_tsv_file, output_yaml_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment