Python Forum
Python3 itertools.groupby printing the key
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python3 itertools.groupby printing the key
#1
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
Reply
#2
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()}")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  itertools and amazing speed Pedroski55 8 1,985 Nov-11-2022, 01:20 PM
Last Post: Gribouillis
  What happens to a <itertools.permutations object at 0x7fe3cc66af68> after it is read? Pedroski55 3 2,354 Nov-29-2020, 08:35 AM
Last Post: DeaD_EyE
  Making lists using itertools and eliminating duplicates. mike3891 2 2,190 Oct-26-2020, 05:39 PM
Last Post: bowlofred
  Generate Cartesian Products with Itertools Incrementally CoderMan 2 1,811 Jun-04-2020, 04:51 PM
Last Post: CoderMan
  itertools.zip_shortest() fo unequal iterators Skaperen 10 6,580 Dec-27-2019, 12:17 AM
Last Post: Skaperen
  Gnuradio python3 is not compatible python3 xmlrpc library How Can I Fix İt ? muratoznnnn 3 4,823 Nov-07-2019, 05:47 PM
Last Post: DeaD_EyE
  can itertools compact a list removing all of some value? Skaperen 6 3,101 Sep-02-2019, 03:19 AM
Last Post: Skaperen
  Help with itertools jarrod0987 1 1,782 Jun-10-2019, 02:41 AM
Last Post: Larz60+
  ImportError: No module named jaraco.itertools in Python manhnt 0 2,940 Nov-08-2018, 11:41 AM
Last Post: manhnt
  Need help with itertools.islice() Charles1 2 2,786 Sep-19-2018, 10:32 AM
Last Post: Charles1

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020