- ( , , ), , '\n', , '- ' ( startswith) , , . :
L = s.splitlines()
it = iter(L)
it = open(....)
for line in it:
if not line.strip():
break
for line in it:
if not line.rstrip():
break
if line.startswith('- '):
data.append(line[:2].rstrip())
else:
raise ValueError, "misformed line %r" % (line,)
: , , . , , "" , , , . , , ( ) :
def getblocks(L):
data = []
block = []
bad = 1
for line in L:
if bad and not line.rstrip():
bad = 0
block = []
continue
if not bad and not line.rstrip():
data.append(block)
block = []
continue
if not bad and line.startswith('- '):
block.append(line[2:].rstrip())
continue
else:
bad = 1
if not bad and block:
data.append(block)
return data
:
>>> L = """hello
...
... - x1
... - x2
... - x3
...
... - x4
...
... - x6
... morning
... - x7
...
... world""".splitlines()
>>> print getblocks(L)
[['x1', 'x2', 'x3'], ['x4']]