Python Forum
Grab and Parse a chunkc of text
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Grab and Parse a chunkc of text
#5
I propose the use of a general function to grab a group of items from a sequence between the first item that satisfies a predicate to the first item that satisfies another predicate. You can easily write a small library for this
from pathlib import Path
import re

class EmptyGroup(RuntimeError):
    pass

def grab(seq, start, stop):
    """Grabs items in a sequence starting with the first
    item for which the predicate start(item) is True until
    the first item if any for which the predicate stop(item)
    is True.
    
    If no item is grabbed, raise the EmptyGroup exception.
    """
    seq = iter(seq)
    for x in seq:
        if start(x):
            yield x
            break
    else:
        raise EmptyGroup
    for x in seq:
        yield x
        if stop(x):
            return

def all_grabbed(seq, start, stop):
    """Generate items extracted from a sequence by calling
    repetively grab(seq, start, stop) until an empty group
    is returned.
    """
    seq = iter(seq)
    try:
        while True:
            yield from grab(seq, start, stop)
    except EmptyGroup:
        pass

def main():
    start = re.compile(r'^Switch').search
    stop = re.compile(r'^$').search
    with Path('paillasse/tmpswitch.log').open() as ifh:
        for x in all_grabbed(ifh, start, stop):
            print(repr(x))

if __name__ == '__main__':
    main()
Here is the contents of paillasse/tmpswitch.log
Output:
Stack# show version Switch Ports Model SW Version SW Image ------ ----- ----- ---------- ---------- * 1 30 WS-C3750E-24PD 12.2(46)SE C3750E-UNIVERSAL-M 2 28 WS-C3750E-24PS 12.2(46)SE C3750E-UNIVERSAL-M 3 54 WS-C3750E-48TD 12.2(46)SE C3750E-UNIVERSAL-M foo bar baz Switch Ports Model SW Version SW Image ------ ----- ----- ---------- ---------- * 1 30 WS-C3750E-24PD 12.2(46)SE C3750E-UNIVERSAL-M 2 28 WS-C3750E-24PS 12.2(46)SE C3750E-UNIVERSAL-M 3 54 WS-C3750E-48TD 12.2(46)SE C3750E-UNIVERSAL-M nooo
and here is the program's output
Output:
'Switch Ports Model SW Version SW Image\n' '------ ----- ----- ---------- ----------\n' '* 1 30 WS-C3750E-24PD 12.2(46)SE C3750E-UNIVERSAL-M\n' '2 28 WS-C3750E-24PS 12.2(46)SE C3750E-UNIVERSAL-M\n' '3 54 WS-C3750E-48TD 12.2(46)SE C3750E-UNIVERSAL-M\n' '\n' 'Switch Ports Model SW Version SW Image\n' '------ ----- ----- ---------- ----------\n' '* 1 30 WS-C3750E-24PD 12.2(46)SE C3750E-UNIVERSAL-M\n' '2 28 WS-C3750E-24PS 12.2(46)SE C3750E-UNIVERSAL-M\n' '3 54 WS-C3750E-48TD 12.2(46)SE C3750E-UNIVERSAL-M\n' '\n'
Reply


Messages In This Thread
Grab and Parse a chunkc of text - by sumncguy - Oct-07-2019, 01:08 PM
RE: Grab and Parse a chunkc of text - by ichabod801 - Oct-07-2019, 02:08 PM
RE: Grab and Parse a chunkc of text - by sumncguy - Oct-07-2019, 03:39 PM
RE: Grab and Parse a chunkc of text - by Gribouillis - Oct-07-2019, 05:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question [PyMuPDF] Grab all strings of a given size? Winfried 3 706 Dec-26-2023, 07:39 AM
Last Post: Pedroski55
  Can't stop keyboard listener to grab chars typed inside CTk window Valjean 9 1,387 Sep-25-2023, 08:07 PM
Last Post: deanhystad
  [SOLVED] [ElementTree] Grab text in attributes? Winfried 3 1,651 May-27-2022, 04:59 PM
Last Post: Winfried
  Screen capture opencv - grab error Kalman15 1 1,631 Jan-27-2022, 12:22 PM
Last Post: buran
  How can I parse a text file? Mike Ru 13 9,155 May-31-2017, 07:39 PM
Last Post: Mike Ru

Forum Jump:

User Panel Messages

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