Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'else:': 0.03; 'subject:text': 0.05; 'initialize': 0.07; 'lines,': 0.07; 'processing.': 0.07; 'subject:bug': 0.07; 'subject:file': 0.07; 'pointers': 0.09; 'cc:addr:python-list': 0.11; 'bug': 0.12; '"-"': 0.16; 'block.': 0.16; 'block:': 0.16; 'emit': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'parser.': 0.16; 'space).': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'bit': 0.19; 'thanks.': 0.20; 'otherwise,': 0.22; 'reset': 0.22; 'cc:addr:python.org': 0.22; 'fairly': 0.24; 'mon,': 0.24; '(or': 0.24; 'cc:2**0': 0.24; 'sort': 0.25; 'task': 0.26; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; '(this': 0.29; 'am,': 0.29; 'character': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'usually': 0.31; 'block,': 0.31; 'end,': 0.31; 'requesting': 0.31; 'file': 0.32; 'text': 0.33; 'beginning': 0.33; "i'd": 0.34; 'basic': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'really': 0.36; 'next': 0.36; 'previous': 0.38; 'rather': 0.38; 'extremely': 0.39; 'sure': 0.39; 'how': 0.40; 'simple,': 0.60; 'skip:o 30': 0.61; 'first': 0.61; 'grab': 0.64; 'jul': 0.74; 'to:none': 0.92 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:cc :content-type:content-transfer-encoding; bh=Ev+8jRYQFfpWzWyNuX13iIvp7F2xoiuo3W3Y7OsU4Gw=; b=P1v29B70Wp8yzFzIDs8wK+y7vcQLWLy2PCQ0kw3HXz0A2m0ONZe7IigMAmEQBCSvrv Cb21B1H0AKxsghxDsblLCW0XBADOpsEUDQ5u7t7amE/AiJZIp5ziVAjZkVYOlYVG/Gxt XXJ06Qmb/uEFVoTkYt/QzCYzKfzjEr60QWjWdXVAkueYej3N1S0wTaWisSJYIdIo5Gsx iLJFuj9TZyDJK+rAsgTcQGrBXiVHECZZKMbIoZyaoIhs6VHxcfoNYVz61LuCbXDHLYrq phd4q2OWsqVg0rltWf6K8Ai1s8bIq4S5bLn41SzTLuyx5Bu20n983TWPWzrnvsXAkvRy pf2g== MIME-Version: 1.0 X-Received: by 10.42.216.148 with SMTP id hi20mr33244554icb.12.1406485032895; Sun, 27 Jul 2014 11:17:12 -0700 (PDT) In-Reply-To: References: Date: Mon, 28 Jul 2014 04:17:12 +1000 Subject: Re: Parse bug text file From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1406485035 news.xs4all.nl 2837 [2001:888:2000:d::a6]:38463 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:75280 On Mon, Jul 28, 2014 at 4:08 AM, CM wrote: > I can go through it with opening the text file and reading in the lines, = and if the first character is a "-" then count that as the start of a bug b= lock, but I am not sure how to find the last line of a bug block...it would= be the line before the first line of the next bug block, but not sure the = best way to go about it. > > There must be a rather standard way to do something like this in Python, = and I'm requesting pointers toward that standard way (or what this type of = task is usually called). Thanks. This is a fairly standard sort of job, but there's not really a ready-to-go bit of code. This is just straight-forward text processing. What I'd do is a stateful parser. Something like this: block =3D None with open("bugs.txt",encoding=3D"utf-8") as f: for line in f: if line.startswith("- "): if block: save_to_database(block) block =3D line else: block +=3D "\n" + line if block: save_to_database(block) # don't forget to grab that last one! This is extremely simple, and you might want to use a regex to look for the upper-case word and date as well (this would falsely notice any description line that happens to begin with a hyphen and a space). But the basic idea is: initialize an accumulator to a null state; whenever you find the beginning of something, emit the previous and reset the accumulator; otherwise, add to the accumulator. At the end, emit any current block. ChrisA