最近有个日志文件太大了,没办法全部读取到内存里,所以用python来readline,太久没用了存个档。
import datetime
import os
sourceFileName='D:/220613.log'
targetFileName='D:/produced.log'
sourceFileData=open(sourceFileName,'r',encoding='utf-8',errors='ignore') #读的时候总遇到某一行字符不对,设置忽略错误
targetFileData=open(targetFileName,'w',encoding='utf-8')
# condition=1;
i=0
while 1:
line=sourceFileData.readline()
# print('row:'+i.__str__())
# # i=i+1
if(line==''): #判断读取完成
break
temp=line.split(' ')
print(temp[1])
timestart=datetime.datetime.strptime('2022-06-13 11:00:00','%Y-%m-%d %H:%M:%S') #字符串与日期类转换
timeend = datetime.datetime.strptime('2022-06-12 14:00:00', '%Y-%m-%d %H:%M:%S')
if(temp[0]=='2022-06-13'):
time=datetime.datetime.strptime(temp[0]+' '+temp[1],'%Y-%m-%d %H:%M:%S')
if((time>=timestart) and (time<=timeend)):
# print("T")
print(line)
targetFileData.write(line)
sourceFileData.close()
targetFileData.close()
文章评论