itertools.groupby https://docs.python.org/3/library/itertools.html?highlight=groupby#itertools.groupby可以帮助:
from io import StringIO
from itertools import groupby
text = '''# abcdefgh
1fnrnf
mrkfr
nfoiernfr
nerfnr
# something
njndjen kj
ejkndjke
#vcrvr'''
with StringIO(text) as file:
lines = (line.strip() for line in file) # removing trailing '\n'
for key, group in groupby(lines, key=lambda x: x[0]=='#'):
if key is True:
# found a line that starts with '#'
print('found header: {}'.format(next(group)))
if key is False:
# group now contanins all lines that do not start with '#'
print('\n'.join(group))
请注意,所有这些都是lazy。您只能将两个标题之间的所有项目存储在内存中。
你必须更换with StringIO(text) as file:
with; with open('test234.txt', 'r') as file:
...
您的测试的输出是:
found header: # abcdefgh
1fnrnf
mrkfr
nfoiernfr
nerfnr
found header: # something
njndjen kj
ejkndjke
found header: #vcrvr
UPDATE正如我误解的那样。这是一个新的尝试:
from io import StringIO
from collections import deque
from itertools import takewhile
from_line = '# abcdefgh'
to_line = '# something'
with StringIO(text) as file:
lines = (line.strip() for line in file) # removing trailing '\n'
# fast-forward up to from_line
deque(takewhile(lambda x: x != from_line, lines), maxlen=0)
for line in takewhile(lambda x: x != to_line, lines):
print(line)
我在哪里使用itertools.takewhile https://docs.python.org/3/library/itertools.html?highlight=takewhile#itertools.takewhile获取行上的迭代器,直到满足条件(直到在您的情况下找到第一个标头)。
the deque
部分只是consume pattern https://docs.python.org/3/library/itertools.html?highlight=consume#itertools-recipesitertools 食谱中建议。它只是快进到给定条件不再成立的点。