I have two CSV files with the same headers. I'd like to compare these files and return entries that are missing from the second file. Here's an example:
file1.csv
fname,lname,city,state
Joe,Smith,Dallas,TX
Jane,Done,Baltimore,MD
Frank,Jones,Plano,TX
file2.csv
fname,lname,city,state
Joe,Smith,Dallas,TX
Jane,Done,Baltimore,MD
Here's my code:
# Returns True if a match is found
# and False if none is found
def find_in_csv(csv_text,search_column,search_string)
csv_text.find {|row|
return row[search_column] == search_string
}
end
How do I extend this function to allow returning missing lines?
This will return an array of rows which are present in file1.csv but missed in file2.csv
csv1 = CSV.read("file1.csv")
csv2 = CSV.read("file2.csv")
csv1 - csv2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With