Python如何在匹配后获取特定数量的行
假设我有一个输入的文本文件,格式如下:
Section1 Heading Number of lines: n1
Line 1
Line 2
...
Line n1
Maybe some irrelevant lines
Section2 Heading Number of lines: n2
Line 1
Line 2
...
Line n2
这个文件的某些部分以一个标题行开始,这个标题行会告诉我这一部分有多少行。每个部分的标题名称都不一样。
我写了一个正则表达式,可以根据用户搜索的标题名称来匹配标题行,然后解析出一个数字 n1/n2 等等,这个数字告诉我这一部分有多少行。我尝试用一个 for-in 循环来读取每一行,直到计数器达到 n1,但到现在为止都没有成功。
我想问的是:当匹配到某一行后,如何根据匹配到的数字返回特定数量的后续行,而这个数字在每个部分都是不同的?我刚开始学习编程,任何帮助都非常感谢。
编辑:好的,这里是我目前写的相关代码:
import re
print
fname = raw_input("Enter filename: ")
toolname = raw_input("Enter toolname: ")
def findcounter(fname, toolname):
logfile = open(fname, "r")
pat = 'SUCCESS Number of lines :'
#headers all have that format
for line in logfile:
if toolname in line:
if pat in line:
s=line
pattern = re.compile(r"""(?P<name>.*?) #starting name
\s*SUCCESS #whitespace and success
\s*Number\s*of\s*lines #whitespace and strings
\s*\:\s*(?P<n1>.*)""",re.VERBOSE)
match = pattern.match(s)
name = match.group("name")
n1 = int(match.group("n1"))
#after matching line, I attempt to loop through the next n1 lines
lcount = 0
for line in logfile:
if line == match:
while lcount <= n1:
match.append(line)
lcount += 1
return result
这个文件本身很长,而且在我感兴趣的部分之间夹杂了很多无关的行。我不太确定的是,如何指定直接打印匹配行后面的行。
2 个回答
0
你可以在生成器里放入这样的逻辑:
def take(seq, n):
""" gets n items from a sequence """
return [next(seq) for i in range(n)]
def getblocks(lines):
# `it` is a iterator and knows where we are in the list of lines.
it = iter(lines)
for line in it:
try:
# try to find the header:
sec, heading, num = line.split()
num = int(num)
except ValueError:
# didnt work, try the next line
continue
# we got a header, so take the next lines
yield take(it, num)
#test
data = """
Section1 Heading 3
Line 1
Line 2
Line 3
Maybe some irrelevant lines
Section2 Heading 2
Line 1
Line 2
""".splitlines()
print list(getblocks(data))
2
# f is a file object
# n1 is how many lines to read
lines = [f.readline() for i in range(n1)]
当然可以!请把你想要翻译的内容发给我,我会帮你用简单易懂的语言解释清楚。