使用ruby/fastercsv在公共字段上合并CSV文件 [英] merge CSV files on a common field with ruby/fastercsv
本文介绍了使用ruby/fastercsv在公共字段上合并CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个主"文件,该文件的列数为:1 2 3 4 5.我还有其他一些文件,其行数少于主文件的行数,每个文件的列数为:1 6.这些文件在第1列字段上匹配,并将第6列添加到主文件中.我看过一些python/UNIX解决方案,但如果合适的话,最好使用ruby/fastercsv.我将不胜感激入门.
I have a 'master' file with a number of columns: 1 2 3 4 5. I have a few other files, with fewer rows than the master file, each with columns: 1 6. I'd like to merge these files matching on the column 1 field and add column 6 to the master. I've seen some python/UNIX solutions but would prefer to use ruby/fastercsv if it's a good fit. I would appreciate any help getting started.
推荐答案
FasterCSV现在是Ruby 1.9中的默认CSV实现.该代码未经测试,但可以正常工作.
FasterCSV is now the default CSV implementation in Ruby 1.9. This code is untested, but should work.
require 'csv'
master = CSV.read('master.csv') # Reads in master
master.each {|each| each.push('')} # Adds another column to all rows
Dir.glob('*.csv').each do |each| #Goes thru all csv files
next if each == 'master.csv' # skips the master csv file
file = CSV.read(each) # Reads in each one
file.each do |line| #Goes thru each line of the file
temp = master.assoc(line[0]) # Finds the appropriate line in master
temp[-1] = line[1] if temp #updates last column if line is found
end
end
csv = CSV.open('output.csv','wb') #opens output csv file for writing
master.each {|each| csv << each} #Goes thru modified master and saves it to file
这篇关于使用ruby/fastercsv在公共字段上合并CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文