I have two text files, file1
and file2
.
File1
contains a bunch of random words, and file2
contains words that I want to remove from file1
when they occur.
Is there a way of doing this?
I know I probably should include my own attempt at a script, to at least show effort, but to be honest it's laughable and wouldn't be of any help.
If someone could at least give a tip about where to start, it would be greatly appreciated.
get the words from each:
if you want unique words from file1 that aren't in file2:
if you care about removing the words from the text of file1
If you read the words into a
set
(one for each file), you can useset.difference()
. This works if you don't care about the order of the output.If you care about the order, read the first file into a list, the second into a set, and remove all the elements in the list that are in the set.
gives:
['a', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog']