Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!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; '"""': 0.05; 'sufficient': 0.05; 'aggregate': 0.07; 'append': 0.07; 'rest,': 0.07; 'valueerror:': 0.07; 'block.': 0.09; 'eof': 0.09; 'file-like': 0.09; 'iterate': 0.09; 'lines:': 0.09; 'seen,': 0.09; 'timestamp': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.14; 'wed,': 0.15; 'output': 0.15; 'represents': 0.15; '"""parse': 0.16; '(however,': 0.16; '*next*': 0.16; 'block:': 0.16; 'class)': 0.16; 'denote': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'generator.': 0.16; 'happily': 0.16; 'helps!': 0.16; 'iterable': 0.16; 'wrote:': 0.16; 'skip': 0.18; 'string,': 0.18; '(or': 0.21; 'cc:2**0': 0.21; 'cc:addr:python.org': 0.21; 'fairly': 0.22; 'object.': 0.22; 'parse': 0.22; 'pipe': 0.22; 'try:': 0.22; 'pass': 0.22; 'am,': 0.23; '2015': 0.23; 'this:': 0.23; 'header :In-Reply-To:1': 0.24; 'sort': 0.25; 'define': 0.27; 'separate': 0.27; 'object,': 0.27; 'yield': 0.27; 'message- id:@mail.gmail.com': 0.28; 'block,': 0.29; 'strings,': 0.29; 'wraps': 0.29; 'lines': 0.30; 'maybe': 0.31; "i'd": 0.31; 'gets': 0.32; 'class': 0.33; 'instead,': 0.33; 'another': 0.34; 'case,': 0.34; 'subject:?': 0.34; 'add': 0.34; 'received:google.com': 0.34; 'lists': 0.34; 'that,': 0.34; 'useful': 0.35; 'could': 0.35; 'instance': 0.35; 'skip:d 30': 0.35; 'something': 0.35; 'list': 0.35; 'but': 0.36; 'list,': 0.36; 'except': 0.36; 'data.': 0.36; 'quite': 0.37; 'should': 0.37; 'subject:: ': 0.37; 'level': 0.37; 'starting': 0.38; "won't": 0.38; 'stuff': 0.38; 'skip:p 20': 0.38; 'end': 0.39; 'method': 0.39; 'does': 0.39; 'takes': 0.39; 'subject:-': 0.39; 'subject:with': 0.40; 'some': 0.40; 'hope': 0.61; 'information,': 0.62; 'information': 0.62; 'details,': 0.65; 'series': 0.65; 'jul': 0.72; 'chrisa': 0.84; 'to:none': 0.90; '1:47': 0.91 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; bh=yifM9+OoG9OJYttH8KLV7bnguj+BapsLsPEfFqwRS5A=; b=FlZDy85qYKOc5FOr0yetIifbEqFy4Eu+tYmYx+ut/XLXYGI2lTkbp6ewe5HBYovvkJ efIEvt2fUBro5hx/Ra8RFO8ZzgV86g+6lyWdALNDbzCfEwpL7CDBvM2A8NDw9XA2rTvD mXvlinuDn6lUFcZt4IAlIQfXCRtu4dD6mgDOk6BMOzCOmU0sKPP0CCNdA3bPZV207Bzg zikKXoTHWdAp1scMMAoSSL2otFmVMKdYxfrnSXJXVvEXe9OTsmI3Ud5PwoLKTsnShwMk IZ9VO0Aj85CBmSUW5iuF1SBKWh1XF6vXvphwN+3HKKhaNJATIC+ICbxr58W93k45uRan TcyQ== MIME-Version: 1.0 X-Received: by 10.42.43.199 with SMTP id y7mr26622611ice.12.1435680161182; Tue, 30 Jun 2015 09:02:41 -0700 (PDT) In-Reply-To: References: Date: Wed, 1 Jul 2015 02:02:41 +1000 Subject: Re: Parsing logfile with multi-line loglines, separated by timestamp? From: Chris Angelico Cc: Python Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 53 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1435680170 news.xs4all.nl 2869 [2001:888:2000:d::a6]:36142 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:93334 On Wed, Jul 1, 2015 at 1:47 AM, Skip Montanaro wrote: > Maybe define a class which wraps a file-like object. Its next() method (or > is it __next__() method?) can just buffer up lines starting with one which > successfully parses as a timestamp, accumulates all the rest, until a blank > line or EOF is seen, then return that, either as a list of strings, one > massive string, or some higher level representation (presumably an instance > of another class) which represents one "paragraph" of iostat output. next() in Py2, __next__() in Py3. But I'd do it, instead, as a generator - that takes care of all the details, and you can simply yield useful information whenever you have it. Something like this (untested): def parse_iostat(lines): """Parse lines of iostat information, yielding ... something lines should be an iterable yielding separate lines of output """ block = None for line in lines: line = line.strip() try: tm = datetime.datetime.strptime(line, "%m/%d/%Y %I:%M:%S %p") if block: yield block block = [tm] except ValueError: # It's not a new timestamp, so add it to the existing block block.append(line) if block: yield block This is a fairly classic line-parsing generator. You can pass it a file-like object, a list of strings, or anything else that it can iterate over; it'll yield some sort of aggregate object representing each time's block. In this case, all it does is append strings to a list, so this will result in a series of lists of strings, each one representing a single timestamp; you can parse the other lines in any way you like and aggregate useful data. Usage would be something like this: with open("logfile") as f: for block in parse_iostat(f): # do stuff with block This will work quite happily with an ongoing stream, too, so if you're working with a pipe from a currently-running process, it'll pick stuff up just fine. (However, since it uses the timestamp as its signature, it won't yield anything till it gets the *next* timestamp. If the blank line is sufficient to denote the end of a block, you could change the loop to look for that instead.) Hope that helps! ChrisA