python词典,don';不要写现有的单词

python词典,don';不要写现有的单词,python,dictionary,Python,Dictionary,我想知道,如果一个单词已经存在于Dico.txt中,它就不会再写了 e = input("Mots: ") f = e.split(" ") with open("Dico.txt", "a") as f2: for line in f: for word in line.split(): f2.write(word + '\n') 您可以为此使用列表: e = i

我想知道,如果一个单词已经存在于Dico.txt中,它就不会再写了

e = input("Mots: ")
f = e.split(" ")

with open("Dico.txt", "a") as f2:
    for line in f:
        for word in line.split():
            f2.write(word + '\n')

您可以为此使用列表:

e = input("Mots: ")
f = e.split(" ")
u = []

with open("Dico.txt", "a") as f2:
    for line in f:
        for word in line.split():
            if not word in u:
              f2.write(word + '\n')
              u.append(word)
            else:
              continue

或者一套。。只需将每个单词添加到一个集合中。并在末尾转换,谢谢,但u.append(word)不起作用。错误:“\u io.TextIOWrapper”对象没有属性“append”
#!/usr/bin/env python

f = open('Dico.txt', 'r')
f2 = open('new.txt', 'w')
w = input('The word? ')
for line in f:
  for word in line.split():
    l = line.replace(w, "")
  f2.write(l.strip(" "))