我是python的初学者,我有一个容量为1TB的日志文件。我想知道如何每行每10秒执行一次日志文件。此外,我希望扫描过程快速而快速。 这是我的代码:
import re
import sys
from csv import writer
import datetime
log_file = '/Users/kiya/Desktop/mysql/ipscan/ip3.txt'
output_file = '/Users/kiya/Desktop/output.csv'
name_to_check = 'MBX_AUTHENTICATION_FAILED'
with open(log_file,encoding="utf-8") as infile:
for line in infile:
if name_to_check in line:
username = re.search(r'(?<=userName=\[)(.*)(?=\],)', line)
username = username.group()
#195347627 *+0900
date = re.search(r'(?P<date>\d{8})\s+(?P<time>\d{9})\s\*\+(?P<zone>\d{4})', line)
date = datetime.datetime.strptime(date.group('date'), "%Y%m%d").strftime("%Y-%m-%d")
#print(date)
time = re.search(r'(?P<date>\d{8})\s+(?P<time>\d{9})\s\*\+(?P<zone>\d{4})', line)
time = datetime.datetime.strptime(time.group('time'), "%H%M%S%f").strftime("%H:%M:%S")
#print(time)
ip = re.search(r'(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])',line)
ip = ip.group()
with open(output_file, "ab", buffering=0) as outfile:
outfile.write(("{},{},{},{}\n".format(username, date, time, ip)).encode())