我有以下列格式的一些數據:發電機功能僅產生第一項
data = """
[Data-0]
Data = BATCH
BatProtocol = DIAG-ST
BatCreate = 20010724
[Data-1]
Data = SAMP
SampNum = 357
SampLane = 1
[Data-2]
Data = SAMP
SampNum = 357
SampLane = 2
[Data-9]
Data = BATCH
BatProtocol = VCA
BatCreate = 20010725
[Data-10]
Data = SAMP
SampNum = 359
SampLane = 1
[Data-11]
Data = SAMP
SampNum = 359
SampLane = 2
"""
結構是:
[Data-x]
其中x是一個數Data =
接着爲無論BATCH
或SAMPLE
- 更多行
我想寫一個函數,爲每個'批'產生一個列表。列表中的第一項是包含行Data = BATCH
的文本塊,列表中的以下項目是包含行Data = SAMP
的文本塊。我現在有
def get_batches(data):
textblocks = iter([txt for txt in data.split('\n\n') if txt.strip()])
batch = []
sample = next(textblocks)
while True:
if 'BATCH' in sample:
batch.append(sample)
sample = next(textblocks)
if 'BATCH' in sample:
yield batch
batch = []
else:
batch.append(sample)
如果這樣調用:
batches = get_batches(data)
for batch in batches:
print batch
print '_' * 20
它,但是,只返回第一個 '批':
['[Data-0]\nData = BATCH\nBatProtocol = DIAG-ST\nBatCreate = 20010724',
'[Data-1]\nData = SAMP\nSampNum = 357\nSampLane = 1',
'[Data-2]\nData = SAMP\nSampNum = 357\nSampLane = 2']
____________________
Wheras我的預期輸出是:
['[Data-0]\nData = BATCH\nBatProtocol = DIAG-ST\nBatCreate = 20010724',
'[Data-1]\nData = SAMP\nSampNum = 357\nSampLane = 1',
'[Data-2]\nData = SAMP\nSampNum = 357\nSampLane = 2']
____________________
['[Data-9]\nData = BATCH\nBatProtocol = VCA\nBatCreate = 20010725',
'[Data-10]\nData = SAMP\nSampNum = 359\nSampLane = 1',
'[Data-11]\nData = SAMP\nSampNum = 359\nSampLane = 2']
____________________
我在想什麼或者如何改善我的功能?
如果你想解析看起來像這樣的文件,看看['ConfigParser'模塊](http://docs.python.org/2/library/configparser.html)。 – Blender 2013-04-09 19:31:23
另外:不是'iter([some listcomp here])',你可以寫'(某個genexp在這裏)'。 – DSM 2013-04-09 19:35:43