Home > Software engineering >  Parse timestamp from a txt file?
Parse timestamp from a txt file?

Time:10-23

I have a large text file on the web that I am using requests to obtain and parse data from. The text file begins each line with a format like [Mon Oct 10 08:58:26 2022]. How can I get the latest 7 days or convert only the datetime to an object or string for storing and parsing later? I simply want to extract the timestamps from the log and print them

CodePudding user response:

You can use TimedRotatingFileHandler for daily or 7-days logs.

read more about timed rotating file handler here

and

read more about extracting timestamps from files

CodePudding user response:

Can you tell me if this snippet solves your problem?

from datetime import datetime

log_line = "[Sun Oct 09 06:14:26 2022] Wiladoc is browsing your wares."
_datetime = log_line[1:25]
_datetime_strp = datetime.strptime(_datetime, '%a %b %d %H:%M:%S %Y')

print(_datetime)
print(_datetime_strp)
  • Related