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

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <jsf80238@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.008
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'subject:text': 0.05; 'attribute': 0.07; 'subject:file': 0.07; 'solution,': 0.09; 'subject:using': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'posted': 0.15; 'escapes': 0.16; 'itertools': 0.16; 'skip:g 40': 0.19; 'import': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'skip:g 30': 0.30; 'message- id:@mail.gmail.com': 0.30; 'text': 0.33; 'skip:# 10': 0.33; 'skip:d 20': 0.34; 'subject:from': 0.34; 'problem': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; '8bit%:9': 0.36; 'hi,': 0.36; 'similar': 0.36; 'list': 0.37; 'skip:& 10': 0.38; 'files': 0.38; 'issue': 0.38; 'skip:p 20': 0.39; '8bit%:6': 0.40; 'easy': 0.60; '8bit%:10': 0.64; 'banner': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=+RCCUMH6/SAOM08vMok63kwmhYzxjvO3MPPVbJVkVcs=; b=xpA+8p4Q7ue3LS+6mmA07k6QBqI6KGOhHxe7UwolJXLav/eH2HdycPbTcIH86iFtu+ eiSKSNMamN+q2A7ahZ2xlFgdhWPbXFBO7VJ0iaLc3KRT+bzjF8bzoWaIsLTzP3UwHicP Bx135JrLss9O4exEMH6FYz165sjD9Jv+I2ujSjeg4Y5gnl7guesnQ0V7AmhAGbTo86j+ JLg2DHtdiKWccej6Y3GU4Y8cC3/05dKLdeaQBDYqbtGx01lPiHSO8vjLCmR4RsYVBwJr o3d78Tbx2D54mj0thr9DRci5uExBBm4E4IuDG2o62cWuXBtjVvEYJQi2/SPt61zVtlZj R7cg==
MIME-Version 1.0
X-Received by 10.50.8.102 with SMTP id q6mr3586549iga.57.1383477157871; Sun, 03 Nov 2013 03:12:37 -0800 (PST)
In-Reply-To <002d01ced358$e18ab5f0$a4a021d0$@org>
References <002d01ced358$e18ab5f0$a4a021d0$@org>
Date Sun, 3 Nov 2013 04:12:37 -0700
Subject Re: Parsing multiple lines from text file using regex
From Jason Friedman <jsf80238@gmail.com>
To Marc <marc@marcd.org>
Content-Type multipart/alternative; boundary=089e0118482c8cc59c04ea43e041
Cc python-list <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1978.1383477167.18130.python-list@python.org> (permalink)
Lines 92
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1383477167 news.xs4all.nl 15994 [2001:888:2000:d::a6]:33180
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:58388

Show key headers only | 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