How to do it...

  1. This recipe will need three components:
        
import re 
        from pathlib import Path 
        import csv

 
  1. Here's the pattern that matches the simple Flask logs. For other kinds of logs, or other formats configured into Flask, a different pattern will be required:
        log_pattern = re.compile( 
            r"\[(?P<timestamp>.*?)\]" 
            r"\s(?P<levelname>\w+)" 
            r"\sin\s(?P<module>[\w\._]+):" 
            r"\s(?P<message>.*)")

 
  1. Here's the function that yields dictionaries for the matching rows. This applies the regular expression pattern. Non-matches are silently skipped. The matches will yield a dictionary of item names and their values:
 def extract_row_iter(source_log_file): for line in source_log_file: match = log_pattern.match(line) if match is None: continue yield ...

Get Modern Python Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.