如何在Python 3中向后搜索几行? [英] How to search backward several lines in Python 3?

查看:755
本文介绍了如何在Python 3中向后搜索几行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在而不是列表:

<$ p从集合中导入deque
N = 100
last_lines = deque(maxlen = N)

作为开放(file.txt)的$ p> (f):
last_lines.append(line)
if'development'in line:
do_something(last_lines)

现在, do_something 会传递给最后100行,包括当前行,如果当前行包含单词 development


There is a solution for searching backward inline in Python Reverse Find in String:

s.rfind('I', 0, index)

But if I need to search for a string in several lines above that line? Say I have found the keyword by using:

with open("file.txt") as f
    searchlines = f.readlines()

for i, line in enumerate(searchlines):
    if "keyword" in line: 
    do_something()

I want do_something() is to find another keyword backward. To apply the code above, I think I need to f.read() so that I can make the file as a string. But this is totally nut, since I have to readlines() and read() the (large) file. I need to use readlines() because the first keyword may appears several times in the text, and I need to find them all.

Is there any better way to do this?

@engineer
- kỹ sư
@engineering
- kỹ thuật
- civil e. ngành xây dựng
- communication e. kỹ thuật thông tin
- control e. kỹ thuật [điều chỉnh, điều khiển] (tự động)
- development e. nghiên cứu những kết cấu mới

解决方案

I'd rather approach this this way: since you want to find the line starting with @, I'd rather store all the lines in a list, then discard the previous lines if a new line that starts with @ is found.

Thus we get:

def do_something(lines):
    print("I've got:")
    print(''.join(lines))

lines = []

with open("file.txt") as f:
    for i, line in enumerate(f):
        if line.startswith('@'):
            lines = []

        lines.append(line)
        if 'development' in line:
            do_something(lines)

The output with file.txt as you have, will be:

I've got:
@engineering
- kỹ thuật
- civil e. ngành xây dựng
- communication e. kỹ thuật thông tin
- control e. kỹ thuật [điều chỉnh, điều khiển] (tự động)
- development e. nghiên cứu những kết cấu mới


In general case if you want to have just N last seen lines, you can use a collections.deque instead of a list:

from collections import deque
N = 100
last_lines = deque(maxlen=N)

with open("file.txt") as f:
    for i, line in enumerate(f):
        last_lines.append(line)
        if 'development' in line:
            do_something(last_lines)

Now the do_something will be passed up to 100 last lines including the current line, if the current line contains the word development.

这篇关于如何在Python 3中向后搜索几行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆