Parsing logfile with multi-line loglines, separated by timestamp?

Chris Angelico rosuav at gmail.com
Wed Jul 1 01:03:06 EDT 2015


On Wed, Jul 1, 2015 at 2:06 PM, Victor Hooi <victorhooi at gmail.com> 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



More information about the Python-list mailing list