1

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.

Yngve
  • 743
  • 4
  • 10
  • 15
  • 3
    Any specific format? One word per line? Randomly structured paragraphs? Anything will do? – André Caron Sep 24 '11 at 04:52
  • ugh, I always leave out relevant information. The format of the output doesn't matter, really. The files themselves are just words separated by spaces. – Yngve Sep 24 '11 at 05:27

2 Answers2

9

get the words from each:

f1 = open("/path/to/file1", "r") 
f2 = open("/path/to/file2", "r") 

file1_raw = f1.read()
file2_raw = f2.read()

file1_words = file1_raw.split()
file2_words = file2_raw.split()

if you want unique words from file1 that aren't in file2:

result = set(file1_words).difference(set(file2_words))

if you care about removing the words from the text of file1

for w in file2_words:
    file1_raw = file1_raw.replace(w, "")
guagay_wk
  • 26,337
  • 54
  • 186
  • 295
MattoTodd
  • 14,467
  • 16
  • 59
  • 76
  • 1
    I don't think you need the list comprehension: `[w for w in file1_raw.split()]` is the same as `file1_raw.split()` – Michael Dunn Sep 24 '11 at 06:55
7

If you read the words into a set (one for each file), you can use set.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.

a = ["a", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog"]
b = {"quick", "brown"}
c = [x for x in a if not x in b]
print c

gives: ['a', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog']

Codie CodeMonkey
  • 7,669
  • 2
  • 29
  • 45
  • Thanks for your reply. I tried both your suggestions, here is what I tried with your second one: `a = [open('c:/file1.txt', 'r')] b = {open('c:/file2.txt', 'r')} c = [x for x in a if not x in b] print(c)` - But I obviously did something wrong, because this is the resulting message: `[<_io.TextIOWrapper name='c:/file1.txt' mode='r' encoding='cp1252'>]` – Yngve Sep 24 '11 at 10:14
  • @Yngve: MattoTodd demonstrated how to read the words, I trust that you worked it out? – Codie CodeMonkey Sep 24 '11 at 21:47