[Tutor] Performing an union of two files containing keywords

Oscar Benjamin oscar.j.benjamin at gmail.com
Mon Feb 17 17:06:10 CET 2014


On 17 February 2014 13:16, Dave Angel <davea at davea.name> wrote:
> On 02/17/2014 06:12 AM, Oscar Benjamin wrote:
>>
>> Something like this:
>>
>> with open(r'D:\file3.txt', 'r+') as fout:
>>      keywords_seen = set()
>>      for filename in r'C:\File1.txt', r'C:\File2.txt':
>>          with open(filename) as fin:
>>              for line in fin:
>>                  keyword = line.strip()
>>                  if keyword not in keywords_seen:
>>                      fout.write(line)
>>                      keywords.add(keyword)

The line above should obviously be keywords_seen.add(keyword)

> You forgot to append the newline to strings in the write() method calls.
>
>                      fout.write(line + "\n")

Iterating over a file keeps the newlines:

>>> list(open('file1.txt'))
['file1\n', 'file2\n']


Oscar


More information about the Tutor mailing list