需要根据日志的时间大小过滤需要的日志,但是原日志的时间格式不能比较大小,所以需要将原日志中定义的时间格式(2018/06/19_08:34:59_313317)转换为时间戳,直接上代码:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import time
import re
def get():
with open('log','r+') as filelog:
filelines=filelog.readlines()
filelog.seek(0,0)
for fileline in filelines:
tss1=fileline.split()[0].split('_')[:2]
tss1=tss1[0]+"_"+tss1[1]
timeArray = time.strptime(tss1,'%Y/%m/%d_%H:%M:%S')
timeStamp = str(time.mktime((timeArray)))
filelog.truncate()
filelog.write(re.sub(fileline.split()[0],timeStamp,fileline))
if __name__ == '__main__':
get()