我正在尝试创建一个函数,它读取一个文本文件,并在另一个文本文件中输入重复的单词一次。问题是这段代码只给出了最后一行重复的单词。
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)
我正在尝试创建一个函数,它读取一个文本文件,并在另一个文本文件中输入重复的单词一次。
你的代码有点不清楚。
如果你想从文件A中提取重复的单词并将其写入文件B,请尝试以下代码。
import string
def repeat_words(in_file, out_file):
with open(in_file, "r") as file_in, open(out_file, "w") as file_out :
seen_words = set()
repeated_words = []
for line in file_in.readlines():
words = line.split()
for word in words:
word = word.lower().strip(string.punctuation)
if word in seen_words and word not in repeat_words:
repeated_words.append(word)
seen_words.add(word)
file_out.write(' '.join(repeat_words))
inF = "nnn.txt"
OutF = "Untitled-1.txt"
repeat_words(inF, OutF)