Last active
September 22, 2018 16:31
-
-
Save orison09/96b1962b5713273aa7436b4ac4551493 to your computer and use it in GitHub Desktop.
TSV and YML Converter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Version 1.1 Basic structure. Added more. | |
require 'yaml' | |
require 'csv' | |
infile = ARGV[0] | |
yaml = File.read(infile) | |
data = YAML.safe_load(yaml) | |
key = data[0].keys | |
outfile = ARGV[1] | |
CSV.open(outfile, 'w', headers: false, col_sep: "\t") do |csv| | |
csv << key | |
data.each do |obj| | |
csv << key.map { |i| obj[i.to_s] } | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Version 1.1 Rubocop. | |
require 'yaml' | |
require 'csv' | |
infile = ARGV[0] | |
data = [] | |
CSV.foreach(infile, headers: true, col_sep: "\t").each do |row| | |
hash = row.to_h | |
data << hash | |
end | |
outfile = ARGV[1] | |
File.write(outfile, data.to_yaml) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment