Python: How to loop through blocks of lines
How to go through blocks of lines separated by an empty line? The file looks like the following: ID: 1 Name: X FamilyN: Y Age: 20 ID: 2 Name: H FamilyN: F Age: 23 ID: 3 Name: S FamilyN: Y Age: 13 ID: 4 Name: M FamilyN: Z Age: 25 I want to loop through the blocks and grab the fields Name, Family name and Age in a list of 3 columns: Y X 20 F H 23 Y S 13 Z M 25 Here's another way, using itertools.groupby . The function groupy iterates through lines of the file and calls isa_group_separator(line) for each line . isa_group_separator returns either True or False (called the key ), and itertools