#!/usr/bin/python3 import re, sys, csv hiring_re = re.compile(r'Who is hiring.*\((?P\S+)\s+(?P\d+)\)') hired_re = re.compile(r'wants to be hired.*\((?P\S+)\s+(?P\d+)\)') n_comments_re = re.compile(r'(\d+) comments') def parse(file, output): context = None for line in file: if mo := hiring_re.search(line): context = ('hiring', mo.group('month'), mo.group('year')) elif mo := hired_re.search(line): context = ('hired', mo.group('month'), mo.group('year')) elif mo := n_comments_re.search(line): if context: output.writerow(context + mo.groups()) else: context = None if __name__ == '__main__': parse(sys.stdin, csv.writer(sys.stdout))