parse date/time from a log entry with only strftime (and no regexen)

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Feb 3 11:48:28 EST 2009


En Tue, 03 Feb 2009 11:52:07 -0200, Simon Mullis <simon at mullis.co.uk>  
escribió:

> I'm writing a script to help with analyzing log files timestamps and  
> have a
> very specific question on which I'm momentarily stumped....
>
> I'd like the script to support multiple log file types, so allow a  
> strftime
> format to be passed in as a cli switch (default is %Y-%m-%d %H:%M:%S).

>>>> import datetime
>>>> p = datetime.datetime.strptime("2008-07-23 12:18:28 this is the
> remainder of the log line that I do not care about", "%Y-%m-%d %H:%M:%S")
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "/opt/local/lib/python2.5/_strptime.py", line 333, in strptime
>     data_string[found.end():])
> ValueError: unconverted data remains:  this is the remainder of the log  
> line
> that I do not care about

If the logfile has fixed width fields, you may ask the user a column range  
to extract the date/time information.

-- 
Gabriel Genellina




More information about the Python-list mailing list