import re import bisect from datetime import datetime f = open('otrs_error.log') matcher = re.compile(r'\[(?P.*?)\]\[(?P.*?)\]\[(?P.*?)\]\[(?P.*?)\] (?P.*)') db = [] lines = f.readlines() f.close() for line in lines: line = line.strip() match = re.match(matcher, line) if match: date = datetime.strptime(match.group('date'), '%c') module = match.group('module') db.append((date.hour, date.minute, date.second, module, line)) else: print(f'Ошибка при разборе строки:\n{line}') db.sort() def search_by_date(hour, minute=None, second=None, data=None): low = [hour] high = [hour] if minute is not None: low.append(minute) high.append(minute) if second is not None: low.append(second) high.append(second + 1) else: high.append(60) else: high.append(60) left = bisect.bisect_left(db, tuple(low)) right = bisect.bisect_left(db, tuple(high)) return [t[4] for t in db[left:right] if data in t[3]]