Jun-15-2017, 04:50 PM
Sure, instead of writing the whole set, iterate over the set and write it one-by-one.
>>> list1 = ["spam", "eggs"] >>> list2 = ["beef", "chicken", "spam"] >>> left_only = set(list1) - set(list2) >>> left_only {'eggs'} >>> with open("left_only.csv", "w") as out: ... for item in left_only: ... print(item, file=out)While we're at it, there's no reason to build a whole list of the file if you're going to only use a set (which contains only unique values, so it uses less memory if there's duplicates).
left = set() #Tell Python what files you want to compare with open('C:\\TEMP\\INDU.csv') as file1: for row in file1: # strip() removes all whitespace, you don't need to specify specifically only newlines value = row.strip() left.add(value)