Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.022 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'elif': 0.04; 'lines,': 0.05; 'interpreted': 0.07; 'newline,': 0.09; 'cc:addr:python- list': 0.10; 'wed,': 0.15; '2:06': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'parser.': 0.16; 'pythonic': 0.16; 'wrote:': 0.16; 'pieces': 0.18; 'cc:2**0': 0.21; 'cc:addr:python.org': 0.21; 'function,': 0.22; 'parse': 0.22; '2015': 0.23; 'seems': 0.24; 'header:In-Reply-To:1': 0.24; 'idea': 0.26; 'message-id:@mail.gmail.com': 0.28; 'itself,': 0.29; 'lines': 0.30; 'putting': 0.31; 'getting': 0.33; 'case,': 0.34; 'subject:?': 0.34; 'this?': 0.34; 'received:google.com': 0.34; 'could': 0.35; 'next': 0.35; 'really': 0.35; 'list': 0.35; 'but': 0.36; 'text': 0.36; 'there': 0.36; 'skip:{ 10': 0.36; 'two': 0.37; 'should': 0.37; 'subject:: ': 0.37; 'skip:g 20': 0.37; 'doing': 0.38; 'pm,': 0.39; 'subject:-': 0.39; 'where': 0.40; 'subject:with': 0.40; 'some': 0.40; 'more': 0.62; 'information': 0.62; 'matter': 0.63; 'you.': 0.64; 'spend': 0.64; 'jul': 0.72; 'chrisa': 0.84; 'victor': 0.84; 'to:none': 0.90; 'device.': 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:cc :content-type; bh=kgJxJ2KZVzCPIcVyuYiGmXSuAQ1o3Fd+8GRd5thj/L8=; b=qtgMBl4VTKO7AmHUGoomK8vejq/slmaJhHrkamVU0Y89XinwJmRtF+zKAftsnP9wP/ cYu2k79xyWXhP8VNhkRhBKgYB8o4tKrssPPF2MWORYIoY6BD3I1TIH+T5Jpa60Psp7+e xLZ294Qe5y2WO4d73JHCDfrHDu6bmE977h9y6Vfp0GiLy8rTGkMt2Xr3ic7jlgP0GZ+d vT1kFzvrsGvtYcRAfTTxYHQJ705a/phxvbP6iJQsnCcg/A0+90vEyVevhZVptkO1QYh3 RIJ8rvUp1jNCilNGcnQCAZu8yeaGM6WCMZqERRutceHWaEzmRmQJ/6UlP5n4jOX7wKYz 5doA== MIME-Version: 1.0 X-Received: by 10.107.8.210 with SMTP id h79mr32507865ioi.27.1435726986960; Tue, 30 Jun 2015 22:03:06 -0700 (PDT) In-Reply-To: <51f65e41-76e9-48c4-8f79-ba4ac060bbe3@googlegroups.com> References: <51f65e41-76e9-48c4-8f79-ba4ac060bbe3@googlegroups.com> Date: Wed, 1 Jul 2015 15:03:06 +1000 Subject: Re: Parsing logfile with multi-line loglines, separated by timestamp? From: Chris Angelico Cc: "python-list@python.org" 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: 27 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1435726989 news.xs4all.nl 2923 [2001:888:2000:d::a6]:33992 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!usenet.pasdenom.info!news.stben.net!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:93357 On Wed, Jul 1, 2015 at 2:06 PM, Victor Hooi wrote: > Aha, cool, that's a good idea =) - it seems I should spend some time getting to know generators/iterators. > > Also, sorry if this is basic, but once I have the "block" list itself, what is the best way to parse each relevant line? > > In this case, the first line is a timestamp, the next two lines are system stats, and then a newline, and then one line for each block device. > > I could just hardcode in the lines, but that seems ugly: > > for block in parse_iostat(f): > for i, line in enumerate(block): > if i == 0: > print("timestamp is {}".format(line)) > elif i == 1 or i == 2: > print("system stats: {}".format(line)) > elif i >= 4: > print("disk stats: {}".format(line)) > > Is there a prettier or more Pythonic way of doing this? This is where you get into the nitty-gritty of writing a text parser. Most of the work is in figuring out exactly what pieces of information matter to you. I recommend putting most of the work into the parse_iostat() function, and then yielding some really nice tidy package that can be interpreted conveniently. ChrisA