Parsing a log file

John Machin sjmachin at
Sun Aug 14 15:01:16 CEST 2005

CG wrote:
> What I basically want to do is end up with a text file that can be
> easily imported into a database with a format like this (or I guess it
> could be written in a SQL script form that could write directly to a
> database like Mysql):
> Connect_Date Connect_Time Disconnect_date Disconnect_time User
> ------------ ------------ --------------- --------------- -------
> 08-13-2005   13:19:37     08-13-2005      15:40:08        John
> * I don't need the fractions of seconds in the timestamps

(1) Famous last words.
(2) What do you gain my throwing information away? Nothing! On input, 
record what you are given. You can always round/truncate on output.

> * I only need date, time, and connect or disconnect, the other info is
> not important

Think about date and time as ONE piece of info. Use a "datetime" object 
in Python, not a "date" and a "time". Same story with the columns in 
your database.

> * If it is possible to calculate the elapsed time between Connect and
> Disconnect and create a new field with that data, that would help (but
> I can easily do that with SQL queries)

and you will be able to do that even more easily if you use one "datetime".

A couple of quick silly questions: What do you do if servers are in 
different timezones? What if "John" connects before a daylight saving 
change and disconnects afterwards? Any chance of your using ISO standard 
  format for representing dates?

More information about the Python-list mailing list