Faster Way To Handle Time String With Python
I have many log files with the format like: 2012-09-12 23:12:00 other logs here and i need to extract the time string and compare the time delta between two log records. I did tha
Solution 1:
You could get rid of the regex and use map
:
date_time = datetime.datetime
for line in log:
date, time = line.strip().split(' ', 2)[:2]
timelist = map(int, date.split('-') + time.split(':'))
d = date_time(*timelist)
- I think
.split(' ', 2)
will be faster than just.split()
because it only splits up to two times and only on spaces, not on any whitespace. map(int, l)
is faster than[int(x) for x in l]
the last time I checked.- If you can, get rid of
.strip()
.
Solution 2:
You could do it entirely with regular expressions, which might be faster.
find_time = re.compile("^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})")
for line in log:
timelist = find_time.match(line)
if timelist:
d = datetime.datetime(*map(int, timelist.groups()))
Post a Comment for "Faster Way To Handle Time String With Python"