I have 2 csv files containing sorted tuples of integers
old file.csv
"(1, 2, 3)","(1, 2, 4)","(1, 3, 5)"
new file.csv
"(1, 2, 3)","(1, 2, 4)"
I want to remove common tuples between these two csv files and print the output as final.csv
Expected Output
"(1,3,5)"
Code Attempt A
import csv
with open('old file.csv', newline ='') as myFile_1:
reader = csv.reader(myFile_1)
list_a = list(reader)
older = [tuple(map(int, i)) for i in list_a]
with open('new file.csv', newline ='') as myFile_2:
reader = csv.reader(myFile_2)
list_b = list(reader)
newer = [tuple(map(int, i)) for i in list_b]
final_output = older.difference(newer)
csvData = [final_output]
with open('final.csv', 'w') as csvFile:
writer = csv.writer(csvFile)
writer.writerows(csvData)
csvFile.close()
Error Type
Exception has occurred: ValueError
invalid literal for int() with base 10: '(1, 2, 3)'
Code Attempt B
import csv
with open('old file.csv', newline ='') as myFile_1:
reader = csv.reader(myFile_1)
list_a = list(reader)
older = [tuple(map(str, i)) for i in list_a]
with open('new file.csv', newline ='') as myFile_2:
reader = csv.reader(myFile_2)
list_b = list(reader)
newer = [tuple(map(str, i)) for i in list_b]
final_output = older.difference(newer)
csvData = [final_output]
with open('final.csv', 'w') as csvFile:
writer = csv.writer(csvFile)
writer.writerows(csvData)
csvFile.close()
Error Type
Exception has occurred: AttributeError
'list' object has no attribute 'difference'
This issue arose when I wanted to manipulate csv files and worked pretty well when the data contained in old.csv and new.csv were generated while running the program and were stored as a variable. This works fine when generating smaller data sets but is extremely problematic when generating large data sets.