解析一个巨大的文本文件并在匹配后获得下一行和前一行

2024-03-28 18:53:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个大约500MB的巨大文本文件,我需要打印与输入匹配的行,以及前3行和后3行

我的文本文件如下所示:

...
...
...
benz is a nice car
...
...
...
its also said benz is a safe car
...
...
...

如果用户输入为'奔驰',那么它应该打印3行之前和之后的匹配,为每个单独的匹配

我的code:- 你知道吗

users= raw_input('enter the word:')
with open('mytext.txt',rb) as f:
     for line if f:
         if users in line:
            print line(i-3)
            print line
            print line(i+3)

但我没有定义错误


Tags: 用户ifislinecodecaruserssafe
3条回答

我编写了一个小函数,可能对您的案例有用:

from collections import deque

def search_cont(filename, search_for, num_before, num_after):
    with open(filename) as f:
        before_lines = deque(maxlen=num_before)
        after_lines = deque(maxlen=num_after+1)
        for _ in range(num_after+1):
            after_lines.append(next(f))
        while len(after_lines)>0:
            current_line = after_lines.popleft()
            if search_for in current_line:
                print("".join(before_lines))
                print(current_line)
                print("".join(after_lines))
                print("           -")
            before_lines.append(current_line)
            try:
                after_lines.append(next(f))
            except StopIteration:
                pass

你把它叫做

search_for = raw_input('enter the word:')
search_cont('mytext.txt', search_for, 3, 3)

这个解决方案没有文件大小的上限(除非你有很长的行),因为内存中永远不会超过7行

使用^{}

$ grep -C 3 benz mytext.txt

您可以从python调用grep

import subprocess
result = subprocess.check_output(["grep" "-A" "3" "-B" "3" "benz" "mytext.txt"])

相关问题 更多 >