I am trying to compare two csv files in python and save the difference to a third csv file in python 2.7.
import csv
f1 = open (\"olddata/file1.csv\")
oldFile1
import csv
def read_csv_file(filename):
res = []
with open(filename) as f:
for line in csv.reader(f):
res.append(line)
oldList1 = read_csv_file("olddata/file1.csv")
oldList2 = read_csv_file("olddata/file2.csv")
difference_list = []
for a,b in zip(oldList1,oldList2):
if a != b:
difference_list.append(a + '\t' + b)
Eventually you have a list of items and you can just write them to file.
EDIT: In this situation, [a,b,c] vs [b,c,a] will fail. If you know that [a,b,c] vs [b,c,a] should return no difference, use the following code pls.
import csv
def read_csv_file(filename):
res = []
with open(filename) as f:
for line in csv.reader(f):
res.append(line)
oldList1 = read_csv_file("olddata/file1.csv")
oldList2 = read_csv_file("olddata/file2.csv")
difference_list = []
for a in oldList1:
for b in oldList2:
if a != b:
difference_list.append(a + '\t' + b)