【问题标题】:deleting some special words of a file and write unique words into a new file删除文件的一些特殊词并将唯一词写入新文件
【发布时间】:2015-11-20 11:22:02
【问题描述】:

我有 2 个文件,一个是包含一些句子的文本文件。另一个是一个文件,其中包含我想从文件中删除它们的单词。首先我必须省略特殊词,然后将唯一词写入一个新文件,每个词在一行中。这是我写的代码。但它不起作用。简单来说,我想先省略一些单词,然后找到唯一的单词。

file1 = open('c:/python34/SimilarityCorpus.txt','r')
file2 = open('c:/python34/ListOfStopWords.txt','r')
file3 = open('c:/python34/Output1.txt','w') 

first_words=[]
second_words=[]
z=[]

for line in file1:  # to write unique words
   for word in line.split():
       if word not in z:
          z.append(word)
for line in file1:
 words = line.split()
  for w in words:
   first_words.append(w)

for line in file2:
  w = line.split()
   for i in w:
    second_words.append(i)

for word1 in first_words :
 for word2 in second_words:
   if word1==word2:
    first_words.remove(word2)

for word in first_words:
 file3.write(word)
 file3.write(' ')

file1.close()
file2.close()
file3.close()

我知道这是基本的,但我是编程新手。

【问题讨论】:

    标签: python


    【解决方案1】:

    欢迎编程!这是一个有趣的世界:)。希望下面的回答对你有所帮助。

    首先,您希望获得每一个独特的词。在这里,set 对象可能对您有用。使用set,您可以遍历每个单词并将其添加到集合中,而不必担心重复。

    z = set()
    for line in file1:  # to write unique words
       for word in line.split():
           z.add(word)
    

    根据我对您的代码的理解,您想找出SimilarityCorpusListOfStopWords 之间的区别,然后将其写入磁盘。既然您只对独特的词感兴趣,而不担心计数,那么sets 可以再次为您提供帮助。

    first_words = set()
    for line in file1:
        words = line.split()
        first_words = first_words.union(words)
    

    在这里,sets().union(other_iterable) 操作简化了迭代新单词的需要。 second_words 也可以这样做。

    最后,您想要获取两组之间的差异,这在 Python 中也可用。为此,您将寻找:

    • first_words 中的单词在 second_words 中不存在,或者
    • second_words 中的单词在 first_words 中不存在。

    在第一种情况下,你会这样做:

    first_words.difference(second_words)
    

    在第二种情况下,你会这样做:

    second_words.difference(first_words)
    

    更多关于集合的文档可以在 Python 文档中找到 here。我会鼓励你使用 Python 3 而不是 2,我认为你是,所以坚持下去!

    要写入磁盘,每个单词换行,您可以执行以下操作:

    for word in first_words:
        file3.write(word)
        file3.write('\n')  # this will write a new line.
    

    目前,您有以下代码模式:

    file3 = open('/path/to/your/file.txt', 'w')
    # do stuff with file3, e.g. write.
    file3.close()
    

    我可能会建议你这样做:

    with open('/path/to/file3.txt', 'w') as file3:
        # do stuff with file3.
    

    这样,你不需要显式地打开和关闭文件; "with open" 行可以自动为您处理。

    我相信你的其余代码是正确的,用于从磁盘读取和写入信息。

    如果您可以更新您的问题以包含有关正在出现的错误的更多详细信息,那将非常有帮助!最后,无论您在这里发现什么最有用的答案,都不要忘记支持/接受它(不一定是我的,我很高兴在这里简单地添加到信息库中并提供帮助)。

    【讨论】:

    • 谢谢。请不要注意文件的名称。 (相似性语料库,...),这段代码是我必须编写的另一个更大代码的一部分。在我从第一个文件中省略第二个文件的单词之后,我只是不知道如何将唯一单词放入文件中。我的意思是应该在哪里写 z = set() for line in file1: for word in line.split(): z.add(word) 到底是什么,最后我应该如何将它们写入文件? (我的意思是代码的最后一部分)。我该如何更改这部分:对于 first_words 中的单词:file3.write(word) file3.write(' ')
    • @sara:我已经更新了我的答案以突出显示如何写入新行。您已正确完成,但在原始代码中省略了写入换行符。我希望这会有所帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-06
    • 1970-01-01
    • 1970-01-01
    • 2020-07-10
    • 1970-01-01
    • 2013-12-03
    相关资源
    最近更新 更多