Created
July 30, 2020 11:29
-
-
Save seahrh/fb395bbe301d7c6348fd3213a28fbf27 to your computer and use it in GitHub Desktop.
Python3 regex match timestamp pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
from typing import List, Set | |
filename = "" | |
out_filename = "" | |
records: List[str] = [] | |
ts_pattern = re.compile(r"^.+(\d{2}/\w{3}/\d{4}:\d{2}:\d{2}:\d{2})") | |
seen: Set[str] = set() | |
with open(filename) as lines: | |
for line in lines: | |
m = ts_pattern.match(line) | |
if m is not None: | |
ts: str = m.group(1) | |
if ts in seen: | |
records.append(ts) | |
else: | |
seen.add(ts) | |
with open(out_filename, mode='wt', encoding='utf-8') as out: | |
out.write('\n'.join(records)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment