如何在计数中间重置计数器?

时间:2019-12-30 20:23:57

标签: python

我正在编写一个程序来读取一个大文档,以某些关键词(±20个单词)为中心提取出一些文本片段。如果两个或两个以上关键字出现在彼此的20个单词之内,则我正在尝试合并这些代码段,以使最终的代码段从第一个关键字之前的20个单词变为最后一个关键字之后的20个单词。

到目前为止,我一直在尝试运行一个计数器,逐字逐句查看代码段,并在到达关键字时进行重置。但是,我遇到了一些困难。到目前为止,这是我的代码

 for m in matches:        #next three lines find each instance of the word, print out surrounding words
            s = ""
            l = " ".join(words[m-20:m+1])
            #while i < 20:
                #k = " ".join(words[m+1:m+i])
                #i = i+1
                #if words[m+i] == word1 or words[m+i] == word2:
                    #i = 0
            j = 20
            i = 0
            while i<20:
                if words[m+i] == word1 or words[m+i] == word2:
                    j = 20+i
                    i = 0
                i = i+1

            k = " ".join(words[m+1:m+j])


            f.write(f"{s}...{l}{k}...")          #writes the data to the external file

这是我尝试过的两种策略,其中一种已被注释掉。有人对如何使这项工作有意见吗?谢谢!!

0 个答案:

没有答案