from itertools import groupby
subject = """ - all the text - """ # read in, or add text here
lines = iter(subject.splitlines())
data = [list(g) for k, g in groupby(lines, lambda x: x.startswith('Cell '))][1:]
cells = [l1 + l2 for l1, l2 in zip(*[iter(data)] * 2)]
for cell in cells:
print cell
作为另一种方法,您可以使用Python的
groupby()
函数来定位块,如下所示:因此,如果
^{pr2}$subject
包含一个文件中的所有文本,那么您将得到四个单元格,每个单元格包含每个单元格的行列表:相关问题 更多 >
编程相关推荐