這裏是我的代碼,但它永遠花費巨大的文件:如何在兩個CSV文件中找到相似的行?
require 'rubygems'
require "faster_csv"
fname1 =ARGV[0]
fname2 =ARGV[1]
if ARGV.size!=2
puts "Display common lines in the two files \n Usage : ruby user_in_both_files.rb <file1> <file2> "
exit 0
end
puts "loading the CSV files ..."
file1=FasterCSV.read(fname1, :headers => :first_row)
file2=FasterCSV.read(fname2, :headers => :first_row)
puts "CSV files loaded"
#puts file2[219808].to_s.strip.gsub(/\s+/,'')
lineN1=0
lineN2=0
# count how many common lines
similarLines=0
file1.each do |line1|
lineN1=lineN1+1
#compare line 1 to all line from file 2
lineN2=0
file2.each do |line2|
puts "file1:l#{lineN1}|file2:l#{lineN2}"
lineN2=lineN2+1
if (line1.to_s.strip.gsub(/\s+/,'') == line2.to_s.strip.gsub(/\s+/,''))
puts "file1:l#{line1}|file2:l#{line2}->#{line1}\n"
similarLines=similarLines+1
end
end
end
puts "#{similarLines} similar lines."
永遠有多久?巨大的巨大? – 2012-02-01 19:20:26
每個csv文件大約需要300000行,並且需要超過30m才能完成 – fenec 2012-02-01 19:22:34
主機上有多少RAM?這些行被加載了多長時間?運行的代碼是什麼操作系統? – 2012-02-01 21:01:51