Python Forum
Python3 itertools.groupby printing the key - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python3 itertools.groupby printing the key (/thread-29073.html)



Python3 itertools.groupby printing the key - august - Aug-17-2020

I'm working with a large text file with the following delimited block of text:
Name: HAGNNESHSSR/3
.......
.......
Name: KAASGEAKPK/2
.......
.......
Name: HKSDFGK/2
.......


I want to parse the block of texts in between the text delimiter "Name:" at the beginning of each line. I also need rest of the Delimiter as they are of unique value.

I'm using the following code to get the delimiter and lines that follow the delim. using itertools.groupby in Python3. There are two such delimited block of texts in the toy input file.

import itertools as it
filename='myData.txt'
with open(filename,'r') as f:
    for key,group in it.groupby(f,lambda line: line.startswith('Name:')):
        print (key) 
However, I'm getting the following output:
Output:
True False True False
Output:
How to get the line which begins with 'Name:'?

Thanks


RE: Python3 itertools.groupby printing the key - bowlofred - Aug-17-2020

The for loop is iterating over every "group" created by groupby. The different groups are "lines that start with Name:" (and the key will be True), and "lines that don't start with Name:" (key will not be True).

Right now you're only printing key, which is just an indication of which group you're in (a Name group, or one without a Name). To get the data in there, you need to look at each of the lines in group

If you happen to have empty blocks, you might have multiple lines in a "True" group, each of which has a "Name:" section.

To extend your code and show the names and the lines in the blocks:

import itertools as it
filename='myData.txt'

section_string = "Name:"

with open(filename,'r') as f:
    for key,group in it.groupby(f,lambda line: line.startswith(section_string)):
        if key:
            for line in group:
                section = line.rstrip().split(section_string,1)[1]
            print(f"Showing the contents of block {section}")
        else:
            for line in group:
                print(f"{section} - {line.rstrip()}")