python - How do I only parse the timestamp from a string -
I am trying to parse only the timestamp from a specific row into a log file via a python. The line from this file is:
March 29 06:12:42 10.11.100.22 [info.events] [Warning] 10.11.100.22: Event, 1234 < P> How do I get a timestamp from this only? I'm using this code on the minute, which is the word 'warning' in it's line from the file, and then gets the timestamp.
def is_Warning (self, line): if line.find ("warning") & gt; = 0: ts = time.strptime (line, "% b% d% h:% M:% S") print "========= ===========% s "% S
When I run this I have a 'ValueError: unchanged data: 10.11.100.22 [info.events] [warning] 10.11.100.22: event, 1234'
itemprop = "text"> use regex
import again ... def is_warning (auto, line): line.find ("warning") & gt; = 0:. Date = re.match (r "[azza-z] {3} \ d {1,2} \ d {2}: \ d {2}: \ d {2}", line) .group () Ts = time.strptime (date, "% b% d% h:% m:% s") print ("======================== =====% "% ts"
Note that
time is actually an old module. You will see the
datetime.datetime.strptime (date, Format) time () if you only have one time.
Comments
Post a Comment