Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #93357
| References | <b8916490-20f3-4070-86dc-821adadd895b@googlegroups.com> <CANc-5Uw-w8pLssy0usPSpnVEPuHJQCz6RJyNfsiHNUwP=KpqpQ@mail.gmail.com> <mailman.192.1435680170.3674.python-list@python.org> <51f65e41-76e9-48c4-8f79-ba4ac060bbe3@googlegroups.com> |
|---|---|
| Date | 2015-07-01 15:03 +1000 |
| Subject | Re: Parsing logfile with multi-line loglines, separated by timestamp? |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.205.1435726989.3674.python-list@python.org> (permalink) |
On Wed, Jul 1, 2015 at 2:06 PM, Victor Hooi <victorhooi@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
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Parsing logfile with multi-line loglines, separated by timestamp? Victor Hooi <victorhooi@gmail.com> - 2015-06-30 08:24 -0700
Re: Parsing logfile with multi-line loglines, separated by timestamp? Chris Angelico <rosuav@gmail.com> - 2015-07-01 02:02 +1000
Re: Parsing logfile with multi-line loglines, separated by timestamp? Victor Hooi <victorhooi@gmail.com> - 2015-06-30 21:06 -0700
Re: Parsing logfile with multi-line loglines, separated by timestamp? Chris Angelico <rosuav@gmail.com> - 2015-07-01 15:03 +1000
csiph-web