更新时间:2023-01-20 10:28:55
只是为了好玩:
# Input file format is tab separated values
# name search_term address code
# Jim jim jim_address 123
# Bob bob bob_address 124
# Lisa lisa lisa_address 126
# Mona mona mona_address 129
infile = File.open("inputfile.tsv")
headers = infile.readline.strip.split("\t")
puts headers.inspect
of = File.new("outputfile.tsv","w")
infile.each_line do |line|
row = line.split("\t")
headers.each_with_index do |key, index|
of.puts "#{key}\t#{row[index]}"
end
end
of.close
# A nicer way, on my machine it does 1.6M rows in about 17 sec
File.open("inputfile.tsv") do | in_file |
headers = in_file.readline.strip.split("\t")
File.open("outputfile.tsv","w") do | out_file |
in_file.each_line do | line |
row = line.split("\t")
headers.each_with_index do | key, index |
out_file << key << "\t" << row[index]
end
end
end
end