Python Forum
File Content comparison-csv
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
File Content comparison-csv
#5
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)
Reply


Messages In This Thread
File Content comparison-csv - by JP_ROMANO - Jun-14-2017, 04:50 PM
RE: File Content comparison-csv - by Ofnuts - Jun-15-2017, 09:30 AM
RE: File Content comparison-csv - by JP_ROMANO - Jun-15-2017, 12:48 PM
RE: File Content comparison-csv - by JP_ROMANO - Jun-15-2017, 02:37 PM
RE: File Content comparison-csv - by nilamo - Jun-15-2017, 04:50 PM
RE: File Content comparison-csv - by JP_ROMANO - Jun-15-2017, 05:34 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020