如何抓取python中的匹配行之后的行 [英] How to grab the lines AFTER a matched line in python
问题描述
我现在是一名业余使用Python的人。对不起,如果这是一个愚蠢的问题,但我想知道如果有人知道一个简单的方法来抓住一堆线,如果输入文件中的格式是这样的:
标题1
第1行
第2行
第3行
标题2
第1行
第2行
第3行
我不会知道每个标题后有多少行,但我想抓住他们。我所知道的是标题的名称或正则表达式模式。
我知道读取文件的唯一方法是for line in file:方式,但是我不知道如何抓住我目前所在的路线。希望这是有道理的,并感谢您的帮助!
*感谢所有的回应!我试图实现一些解决方案,但我的问题是,不是所有的标题是相同的名称,我不知道如何解决它。我需要一个不同的正则表达式...任何建议?
生成函数
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.
这篇关于如何抓取python中的匹配行之后的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!