Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Identifying different date patterns in python list

I have this sample logs data in a list

data = ["[2022-08-15 17:42:32,436: INFO/MainProces] lorqw q addadasdasdasdad",
"2022-10-24T13:29:50.579Z dasdadasdasdadadadadaddada",
"asdadadad adasdas3453 454234 fsdf53",
"Mon, 24 Oct 2022 13:29:48 GMT express:router expressInit : /health",
'time="2022-10-24T13:29:12Z" level=error msg="checking config status failed: sdadasd"',
"2022/10/24 13:29:15 [error] 234 ssdfsd 435345"]

what I tried so far to print the item if the date is exist along with it’s index

for index, elem in enumerate(data):
    if ']' and '[' in elem:
        print(f'Date found at index: {index}')

current output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Date found at index: 0
Date found at index: 5

Expected Output:

Date found at index: 0
Date found at index: 1
Date found at index: 3
Date found at index: 4
Date found at index: 5

>Solution :

Since the only real repeating part of the date is the time, I’d chase the time with a regex:

for index, entry in enumerate(data):
  if re.search(r'(\s|T)[0-9]{2}\:[0-9]{2}\:[0-9]{2}([\.\,][0-9]+)*', entry):
    print(f"Found date in {index}")

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading