Created
March 16, 2012 04:11
-
-
Save hlxwell/2048474 to your computer and use it in GitHub Desktop.
CSV Comparer
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
require 'rubygems' | |
require 'csv' | |
def normalize(row) | |
row[2].sub!(/^0+/, "") if row[2] | |
row[3].sub!(/^0+/, "") if row[3] | |
return row | |
end | |
puts "start..." | |
old_rows = CSV.read("data_from_user.csv") | |
new_rows = CSV.read("data_from_server.csv") | |
old_rows.each { |row| normalize(row) } | |
new_rows.each { |row| normalize(row) } | |
rows = [] << old_rows << new_rows | |
contrast_table = rows.transpose.map do |old_row, new_row| | |
tmp_row = Array.new(old_row.size, "") | |
tmp_row.each_index do |index| | |
# date | |
if index == 0 | |
tmp_row[index] = "#{old_row[index]}" | |
next | |
end | |
if old_row[index] != new_row[index] && (old_row[index].to_s != '0' && new_row[index].to_s != '0') | |
case index | |
when 12 | |
new_row_data = new_row[index] ? "#{new_row[4]} * #{new_row[11]} = #{new_row[12]}" : "null" | |
tmp_row[index] = "#{old_row[index]||"null"} => #{new_row_data}" | |
when 5 | |
if new_row[index].nil? or new_row[index].to_f.round(1) != old_row[index].to_f | |
tmp_row[index] = "#{old_row[index]||"null"} => #{new_row[index]||"null"}" | |
end | |
else | |
tmp_row[index] = "#{old_row[index]||"null"} => #{new_row[index]||"null"}" | |
end | |
end | |
end | |
tmp_row | |
end | |
CSV.open("different_or_missing_from_user_exported_data.csv", "w") do |csv| | |
contrast_table.each { |a| | |
csv << a | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment