CodexBloom - Programming Q&A Platform

Parsing Custom Log Files with Python - Errors on Timestamp Format Variations

👀 Views: 276 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-14
python regex datetime parsing Python

I just started working with I'm building a feature where I'm sure I'm missing something obvious here, but I'm trying to parse a custom log file format in Python, but I'm running into issues with varying timestamp formats that cause my parsing logic to unexpected result intermittently..... The log files contain lines like these: ``` 2023-03-15 14:23:45 INFO User logged in 2023-03-15 14:23:46 WARNING Low disk space 15/03/2023 14:23:47 behavior Disk write failure ``` I need to extract the timestamp and associated message, but the second line uses a different date format. My initial approach was to use regular expressions to match the timestamps, but I'm getting `ValueError: time data '15/03/2023' does not match format '%Y-%m-%d %H:%M:%S'` for the second line. Here's the code I'm using: ```python import re from datetime import datetime log_lines = [ '2023-03-15 14:23:45 INFO User logged in', '2023-03-15 14:23:46 WARNING Low disk space', '15/03/2023 14:23:47 behavior Disk write failure' ] pattern = r'(?P<timestamp>[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}|\d{2}/\d{2}/\d{4} \d{2}:\d{2}:\d{2}]) (?P<level>[A-Z]+) (?P<message>.*)' for line in log_lines: match = re.match(pattern, line) if match: timestamp_str = match.group('timestamp') # Attempt to parse timestamp timestamp = datetime.strptime(timestamp_str, '%Y-%m-%d %H:%M:%S') print(timestamp, match.group('level'), match.group('message')) ``` I've tried modifying the regex to match both date formats, but I'm struggling with the parsing logic afterward. Any suggestions on how to handle this more gracefully? Should I implement a try-except block, or is there a more elegant solution? My current approach feels a bit clunky, and I'm looking for best practices in parsing such log files effectively. Any help would be appreciated! This is part of a larger API I'm building. Any help would be greatly appreciated! I'm working in a Linux environment. Any examples would be super helpful. I'm coming from a different tech stack and learning Python. I'd be grateful for any help. My development environment is Ubuntu 22.04.