答案 0 :(得分:0)
使用itertools.takewhile
和itertools.dropwhile
。我假设只有虚线可以用破折号开头。
from itertools import takewhile, dropwhile
file = iter('''---
data1
data2
---
stuff
---
data3
data4
---'''.splitlines())
def in_between(data):
data = iter(data) # make sure data is iterator
not_dashed = lambda line: not line.startswith('-')
while True:
data = dropwhile(not_dashed, data)
next(data)
for x in takewhile(not_dashed, data):
yield x
next(data)
for x in in_between(file):
print x
输出:
data1
data2
data3
data4
由于此解决方案从不将整个文件读入内存,而是一次使用迭代器和一行,因此处理非常大的文件时应该没有问题。