大佬麻烦看看这个
import refrom datetime import datetime
def save_logs_to_file(log_file, start_time, end_time, output_file):
logs_within_range = []
pattern = r'\[(.*?)\]'# 正则表达式模式,用于提取日志中的时间戳
with open(log_file, 'r') as file:
for line in file:
timestamp_match = re.search(pattern, line)
if timestamp_match:
timestamp_str = timestamp_match.group(1)
log_time = datetime.strptime(timestamp_str, '%Y-%m-%d %H:%M:%S')# 解析时间戳为 datetime 对象
if start_time <= log_time <= end_time:
logs_within_range.append(line.strip())
with open(output_file, 'w') as outfile:
for log in logs_within_range:
outfile.write(log + '\n')
def read_time_from_file(start_time_file, end_time_file):
with open(start_time_file, 'r') as start_file, open(end_time_file, 'r') as end_file:
start_time_str = start_file.readline().strip()
end_time_str = end_file.readline().strip()
# 清理时间字符串中的额外字符
start_time_str = start_time_str.strip()
end_time_str = end_time_str.strip()
# 检查时间字符串长度是否符合要求
if len(start_time_str) != len('YYYY-MM-DD HH:MM:SS') or len(end_time_str) != len('YYYY-MM-DD HH:MM:SS'):
raise ValueError('时间字符串格式不正确')
start_time = datetime.strptime(start_time_str, '%Y-%m-%d %H:%M:%S')
end_time = datetime.strptime(end_time_str, '%Y-%m-%d %H:%M:%S')
return start_time, end_time
log_file = r'C:\Users\huawei\Desktop\server.log'
start_time_file = r'C:\Users\huawei\Desktop\start_time.txt'# 开始时间文件路径
end_time_file = r'C:\Users\huawei\Desktop\end_time.txt'# 结束时间文件路径
output_file = r'C:\Users\huawei\Desktop\1.txt'# 输出文件路径
start_time, end_time = read_time_from_file(start_time_file, end_time_file)
save_logs_to_file(log_file, start_time, end_time, output_file)
时间为 2022-06-30 10:00:002022-06-30 12:00:00
报错内容是 (data_string, format))
ValueError: time data ' 01' does not match format '%Y-%m-%d %H:%M:%S'
麻烦大佬看看
问题出在读取开始时间和结束时间的文件时,存在额外的空格字符导致时间字符串格式不正确。在读取时间字符串前,需要清理字符串中的额外字符。修改read_time_from_file函数的代码如下:
def read_time_from_file(start_time_file, end_time_file):
with open(start_time_file, 'r') as start_file, open(end_time_file, 'r') as end_file:
start_time_str = start_file.readline().strip()
end_time_str = end_file.readline().strip()
# 清理时间字符串中的额外字符
start_time_str = start_time_str.strip()
end_time_str = end_time_str.strip()
# 检查时间字符串长度是否符合要求
if len(start_time_str) != len('YYYY-MM-DD HH:MM:SS') or len(end_time_str) != len('YYYY-MM-DD HH:MM:SS'):
raise ValueError('时间字符串格式不正确')
start_time = datetime.strptime(start_time_str, '%Y-%m-%d %H:%M:%S')
end_time = datetime.strptime(end_time_str, '%Y-%m-%d %H:%M:%S')
return start_time, end_time
这样修改后,再次运行代码应该就不会报错了。 看到你的代码,问题出在读取时间字符串时,没有正确处理字符串中的额外字符和空白字符。你可以尝试修改read_time_from_file函数中的代码如下:
def read_time_from_file(start_time_file, end_time_file):
with open(start_time_file, 'r') as start_file, open(end_time_file, 'r') as end_file:
start_time_str = start_file.readline().strip()
end_time_str = end_file.readline().strip()
# 清理时间字符串中的额外字符和空白字符
start_time_str = start_time_str.replace('\ufeff', '').strip()
end_time_str = end_time_str.replace('\ufeff', '').strip()
start_time_str = start_time_str.strip()
end_time_str = end_time_str.strip()
# 检查时间字符串长度是否符合要求
if len(start_time_str) != len('YYYY-MM-DD HH:MM:SS') or len(end_time_str) != len('YYYY-MM-DD HH:MM:SS'):
raise ValueError('时间字符串格式不正确')
start_time = datetime.strptime(start_time_str, '%Y-%m-%d %H:%M:%S')
end_time = datetime.strptime(end_time_str, '%Y-%m-%d %H:%M:%S')
return start_time, end_time
这里使用了replace函数将\ufeff(BOM)字符替换为空白字符,并使用strip函数去除字符串两端的空格。这样可以确保时间字符串不包含额外的字符或空白字符。然后再进行长度检查和解析时间字符串为datetime对象的操作。
你可以尝试运行修改后的代码,看看是否解决了问题。如果还有其他问题,请随时提问。
球一个最佳答案谢谢啦!这对我非常重要!{:10_254:}{:10_254:}{:10_254:} Traceback (most recent call last):
File "D:\软件\djangoProject\app\日志切分.py", line 64, in <module>
save_logs_to_file(log_file, start_time, end_time, output_file)
File "D:\软件\djangoProject\app\日志切分.py", line 37, in save_logs_to_file
log_time = datetime.strptime(timestamp_str, '%Y-%m-%d %H:%M:%S')# 解析时间戳为 datetime 对象
File "C:\Users\huawei\AppData\Local\Programs\Python\Python37\lib\_strptime.py", line 577, in _strptime_datetime
tt, fraction, gmtoff_fraction = _strptime(data_string, format)
File "C:\Users\huawei\AppData\Local\Programs\Python\Python37\lib\_strptime.py", line 359, in _strptime
(data_string, format))
ValueError: time data ' 01' does not match format '%Y-%m-%d %H:%M:%S' 还是不行 isdkz 发表于 2023-11-5 18:05
问题出在读取开始时间和结束时间的文件时,存在额外的空格字符导致时间字符串格式不正确。在读取时间字符串 ...
Traceback (most recent call last):
File "D:\软件\djangoProject\app\日志切分.py", line 64, in <module>
save_logs_to_file(log_file, start_time, end_time, output_file)
File "D:\软件\djangoProject\app\日志切分.py", line 37, in save_logs_to_file
log_time = datetime.strptime(timestamp_str, '%Y-%m-%d %H:%M:%S')# 解析时间戳为 datetime 对象
File "C:\Users\huawei\AppData\Local\Programs\Python\Python37\lib\_strptime.py", line 577, in _strptime_datetime
tt, fraction, gmtoff_fraction = _strptime(data_string, format)
File "C:\Users\huawei\AppData\Local\Programs\Python\Python37\lib\_strptime.py", line 359, in _strptime
(data_string, format))
ValueError: time data ' 01' does not match format '%Y-%m-%d %H:%M:%S'
不行 import re
from datetime import datetime
def save_logs_to_file(log_file, start_time, end_time, output_file):
logs_within_range = []
# pattern = r'\[(.*?)\]'# 正则表达式模式,用于提取日志中的时间戳
pattern = r'\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\]'
with open(log_file, 'r') as file:
for line in file:
timestamp_match = re.search(pattern, line)
if timestamp_match:
timestamp_str = timestamp_match.group(1)
log_time = datetime.strptime(timestamp_str, '%Y-%m-%d %H:%M:%S')# 解析时间戳为 datetime 对象
if start_time <= log_time <= end_time:
logs_within_range.append(line.strip())
with open(output_file, 'w') as outfile:
for log in logs_within_range:
outfile.write(log + '\n')
def read_time_from_file(start_time_file, end_time_file):
with open(start_time_file, 'r') as start_file, open(end_time_file, 'r') as end_file:
start_time_str = start_file.readline().strip()
end_time_str = end_file.readline().strip()
# 清理时间字符串中的额外字符
start_time_str = start_time_str.strip()
end_time_str = end_time_str.strip()
# 检查时间字符串长度是否符合要求
if len(start_time_str) != len('YYYY-MM-DD HH:MM:SS') or len(end_time_str) != len('YYYY-MM-DD HH:MM:SS'):
raise ValueError('时间字符串格式不正确')
start_time = datetime.strptime(start_time_str, '%Y-%m-%d %H:%M:%S')
end_time = datetime.strptime(end_time_str, '%Y-%m-%d %H:%M:%S')
return start_time, end_time
log_file = r'C:\Users\huawei\Desktop\server.log'
start_time_file = r'C:\Users\huawei\Desktop\start_time.txt'# 开始时间文件路径
end_time_file = r'C:\Users\huawei\Desktop\end_time.txt'# 结束时间文件路径
output_file = r'C:\Users\huawei\Desktop\1.txt'# 输出文件路径
start_time, end_time = read_time_from_file(start_time_file, end_time_file)
save_logs_to_file(log_file, start_time, end_time, output_file)
改成这样就可以了
页:
[1]