Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Writing repeating words in a line in a textfile to another textfile

$
0
0

I'm trying to make a function where it reads a textfile and it inputs the repeated words once in another textfile. The problem is that this code just gives the last line repeated words.

import string  def repeat_words(in_file, out_file):     file_in = open(in_file, "r")     file_out = open(out_file, "w")     for lines in file_in:     words = lines.lower().split()     seen_words = []     repeat_words = []         for word in words:            word = word.strip(string.punctuation)            if word in seen_words and word not in repeat_words:                repeat_words.append(word)            seen_words.append(word) file_out.write(''.join(repeat_words) +'\n') file_in.close() file_out.close()inF = "nnn.txt"OutF = "Untitled-1.txt"repeat_words(inF, OutF)

Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>