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


Groups > comp.lang.python > #58388 > unrolled thread

Re: Parsing multiple lines from text file using regex

Started byJason Friedman <jsf80238@gmail.com>
First post2013-11-03 04:12 -0700
Last post2013-11-03 04:12 -0700
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#58388 — Re: Parsing multiple lines from text file using regex

FromJason Friedman <jsf80238@gmail.com>
Date2013-11-03 04:12 -0700
SubjectRe: Parsing multiple lines from text file using regex
Message-ID<mailman.1978.1383477167.18130.python-list@python.org>

[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])

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web