Parsing a log file

Andreas Kostyrka andreas at kostyrka.org
Sun Aug 14 23:11:21 CEST 2005


Completly untested:

#!/usr/bin/env python

import sys, datetime

user = sys.argv[1]

starttime = None
for l in sys.stdin:
    flds = l.strip().split()
    datestr, timestr, prog, op, to, sname = flds
    month, day, year = [int(x) for x in datestr.split("-", 2)]
    hour, min, sec, ms = [int(x) for x in timestr.split(":")]
    timestamp = datetime.datetime(year, month, day, hour, min, sec)
    if op == 'CONNECTED':
        assert starttime is None
        starttime = timestamp
    elif op == 'DISCONNECTED':
        assert starttime is not None
        endtime = timestamp
        sql = "insert into data (start, end, user) value (%r, %r, %r);"
        print sql % (starttime, endtime, user)
    else:
        raise AssertationError("%r is not a valid line" % l)



Am Sonntag, den 14.08.2005, 07:31 -0700 schrieb CG:
> Thanks Andreas,
> 
> In your first paragraph, you ask about incorrect input.  I guess it is
> possible, but without that information, my collection of the data is
> useless, so I really don't know what I would do with that.
> 
> As for the other stuff, I can hack the data in other ways, such as with
> VBA and MSAccess, which I am more familiar with, but I am trying to
> move to Linux and want to do it right the first time.  I figure Perl is
> the more common language for this kind of stuff, but I did want to try
> to learn some Python while I am at it.  I have started the tutorial,
> but being a businessman, time is an issue, which, if I had an example
> script that did a similar thing, I can learn by doing that (I am
> looking for something similar now).
> 
> I do live in a low-labor cost country, so I can hire someone to do it
> for a small amount of money, but Python people are a little harder to
> find.
> 
> Thanks for the comments,
> Clint
> 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 196 bytes
Desc: Dies ist ein digital signierter Nachrichtenteil
URL: <http://mail.python.org/pipermail/python-list/attachments/20050814/6610bfba/attachment.sig>


More information about the Python-list mailing list