且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何抓取python中的匹配行之后的行

更新时间:2023-12-03 16:57:52

生成函数

pre $ def group_by_heading(some_source):
buffer = []
for some_source:
if line.startswith(Heading):
if buffer:yield buffer
buffer = [line]
else:
buffer.append(line)
产生缓冲区

以open(some_file,r)作为源:
在group_by_heading(source)中为heading_and_lines:
heading = heading_and_lines [0]
lines = heading_and_lines [1:]
#处理完毕。


I am an amateur using Python on and off for some time now. Sorry if this is a silly question, but I was wondering if anyone knew an easy way to grab a bunch of lines if the format in the input file is like this:

" Heading 1

Line 1

Line 2

Line 3

Heading 2

Line 1

Line 2

Line 3 "

I won't know how many lines are after each heading, but I want to grab them all. All I know is the name, or a regular expression pattern for the heading.

The only way I know to read a file is the "for line in file:" way, but I don't know how to grab the lines AFTER the line I'm currently on. Hope this makes sense, and thanks for the help!

*Thanks for all the responses! I have tried to implement some of the solutions, but my problem is that not all the headings are the same name, and I'm not sure how to work around it. I need a different regular expression for each... any suggestions?*

Generator Functions

def group_by_heading( some_source ):
    buffer= []
    for line in some_source:
        if line.startswith( "Heading" ):
            if buffer: yield buffer
            buffer= [ line ]
        else:
            buffer.append( line )
    yield buffer

with open( "some_file", "r" ) as source:
    for heading_and_lines in group_by_heading( source ):
        heading= heading_and_lines[0]
        lines= heading_and_lines[1:]
        # process away.