您可以使用itertools.groupby对
:Entry列表中出现的行进行分组:
import itertools as itfilename='test.dat'with open(filename,'r') as f: for key,group in it.groupby(f,lambda line: line.startswith(':Entry')): if not key: group = list(group) print(group)
产量
['- Namen', 'John Doen', 'n', '- Daten', '20/12/1979n']['n', '-Namen', 'Jane Doen', '- Daten', '21/12/1979n']
或者,要处理组,您实际上不需要转换
group为列表:
with open(filename,'r') as f: for key,group in it.groupby(f,lambda line: line.startswith(':Entry')): if not key: for line in group: ...
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)