Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #58388

Re: Parsing multiple lines from text file using regex

References <002d01ced358$e18ab5f0$a4a021d0$@org>
Date 2013-11-03 04:12 -0700
Subject Re: Parsing multiple lines from text file using regex
From Jason Friedman <jsf80238@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1978.1383477167.18130.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

>  Hi,
> I am having an issue with something that would seem to have an easy
> solution, but which escapes me.  I have configuration files that I would
> like to parse.  The data I am having issue with is a multi-line attribute
> that has the following structure:
>
> banner <option> <banner text delimiter>
> Banner text
> Banner text
> Banner text
> ...
> <banner text delimiter>
>
This is an alternative solution someone else posted on this list for a
similar problem I had:

#!/usr/bin/python3
from itertools import groupby
def get_lines_from_file(file_name):
    with open(file_name) as reader:
        for line in reader.readlines():
            yield(line.strip())

counter = 0
def key_func(x):
    if x.strip().startswith("banner") and x.strip().endswith("<banner text
delimiter>"):
        global counter
        counter += 1
    return counter

for key, group in groupby(get_lines_from_file("my_data"), key_func):
    print(list(group)[1:-1])

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Parsing multiple lines from text file using regex Jason Friedman <jsf80238@gmail.com> - 2013-11-03 04:12 -0700

csiph-web