Reputation: 109
First File | Second File
bob | greg
bob | larry
mark | mark
larry | bruce
tom | tom
With the code bellow I get output: bob, but I need to get bob x 2
with open('some_file_1.txt', 'r') as file1:
with open('some_file_2.txt', 'r') as file2:
diff = set(file1).difference(file2)
with open('some_output_file.txt', 'w') as file_out:
for line in same:
file_out.write(line)
Upvotes: 0
Views: 50
Reputation: 54213
Sounds like you want the difference of two collections.Counter
objects.
import collections
with open("file1.txt") as f1, open("file2.txt") as f2:
c1, c2 = collections.Counter(f1), collections.Counter(f2)
result = c1 - c2
# Counter({"bob": 2})
with open("output.txt", "w") as outf:
for line, count in result.items():
outf.write("{} x {}".format(line, count))
Upvotes: 2