Python,在匹配前后提取3行

时间:2017-07-24 23:37:19

标签: python

我试图找出如何在匹配的单词之前和之后提取3行。

此刻,我的话被发现了。我写了一些文本来测试我的代码。并且,我想出了在比赛结束后如何打印三条线。

但是,我很难弄清楚如何在单词“secure”之前打印三行。

这是我到目前为止所做的:

from itertools import islice
with open("testdoc.txt", "r") as f:
for line in f:
    if "secure" in line:
        print("".join(line))
        print ("".join(islice(f,3)))

以下是我为测试而创建的文字:

----------------------------
 This is a test to see
if i can extract information
using this code
I hope, I try, 
maybe secure shell will save thee
Im adding extra lines to see my output
hoping that it comes out correctly
boy im tired, sleep is nice
until then, time will suffice

2 个答案:

答案 0 :(得分:1)

我提出了这个解决方案,只是在列表中添加前面的行,并在4个元素之后删除第一行

from itertools import islice

with open("testdoc.txt", "r") as f:
    linesBefore = list()
    for line in f:
        linesBefore.append(line.rstrip())
        if len(linesBefore) > 4: #Adding up to 4 lines
            linesBefore.pop(0)
        if "secure" in line:
            if len(linesBefore) == 4: # if there are at least 3 lines before the match
                for i in range(3):
                    print(linesBefore[i])
            else: #if there are less than 3 lines before the match
                print(''.join(linesBefore))
            print("".join(line.rstrip()))
            print ("".join(islice(f,3)))

答案 1 :(得分:0)

您需要缓冲线条,以便您可以回忆它们。最简单的方法是将所有行加载到列表中:

with open("testdoc.txt", "r") as f:
    lines = f.readlines()  # read all lines into a list
    for index, line in enumerate(lines):  # enumerate the list and loop through it
        if "secure" in line:  # check if the current line has your substring
            print(line.rstrip())  # print the current line (stripped off whitespace)
            print("".join(lines[max(0,index-3):index]))  # print three lines preceeding it

但是如果你需要最大的存储效率,你可以使用缓冲区来存储最后3行,因为你逐行循环遍历文件。 collections.deque是理想的选择。