I have a text file that looks like this, with blocks of lines separated by blank lines:
JavaScript
x
20
20
1
ID: 1
2
Name: X
3
FamilyN: Y
4
Age: 20
5
6
ID: 2
7
Name: H
8
FamilyN: F
9
Age: 23
10
11
ID: 3
12
Name: S
13
FamilyN: Y
14
Age: 13
15
16
ID: 4
17
Name: M
18
FamilyN: Z
19
Age: 25
20
How can I loop through the blocks and process the data in each block? eventually I want to gather the name, family name and age values into three columns, like so:
JavaScript
1
5
1
Y X 20
2
F H 23
3
Y S 13
4
Z M 25
5
Advertisement
Answer
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.groupby
then groups all the consecutive lines that yielded the same True or False result.
This is a very convenient way to collect lines into groups.
JavaScript
1
21
21
1
import itertools
2
3
def isa_group_separator(line):
4
return line=='n'
5
6
with open('data_file') as f:
7
for key,group in itertools.groupby(f,isa_group_separator):
8
# print(key,list(group)) # uncomment to see what itertools.groupby does.
9
if not key: # however, this will make the rest of the code not work
10
data={} # as it exhausts the `group` iterator
11
for item in group:
12
field,value=item.split(':')
13
value=value.strip()
14
data[field]=value
15
print('{FamilyN} {Name} {Age}'.format(**data))
16
17
# Y X 20
18
# F H 23
19
# Y S 13
20
# Z M 25
21