Jody
Jody

Reputation: 115

How do I compare a .txt to another and return what is not in it?

I am trying to compare a .txt file to another and return what is not in it.

For example

one.txt

a
b
c
d

two.txt

b
c
d
e

output

e

I have tried using symmetric_difference() but this will return the difference between both of them. Using the example, it will return e and a.

with open('text_one.txt', 'r') as file1:
    with open('text_two.txt', 'r') as file2:
        same = set(file1).symmetric_difference(file2)

same.discard('\n')

with open('output.txt', 'w') as file_out:
    for line in same:
        file_out.write(line)

Upvotes: 1

Views: 94

Answers (2)

Michael Szczesny
Michael Szczesny

Reputation: 5026

I think you should take care of newline characters before comparing the sets of lines to avoid unexpected output if an equal line is the last item in one file. b difference a will return only the missing items in a.

strip = lambda x: x.strip()
with open('one.txt') as f1, open('two.txt') as f2, open('output.txt', 'w') as out:
    out.write(
        '\n'.join(
        set(map(strip, f2))
            .difference(map(strip, f1))))

For the output I'm assuming you want every missing line as a new line.

output.txt

e

Upvotes: 0

IoaTzimas
IoaTzimas

Reputation: 10624

If you want the items of file2 that are not in file1,just replace this:

same = set(file1).symmetric_difference(file2)

by this:

same = set(file2)-set(file1)

Upvotes: 2

Related Questions