AlexW
AlexW

Reputation: 2587

Python - Find date from string

Would anyone know a regex string or another method of obtaining the date and time from this string into variables? The position of the string could change, so line and char no would not work. This is a bit beyond my ability, can it be done?

Thanks

Dear Customer,

(Call Transferred) Start Time & Date: 00:05 Monday 6th February 2017               
Completion Time & Date: 06:00 Monday 6th February 2017                 

Details of Work:

Upvotes: 0

Views: 957

Answers (1)

Tagc
Tagc

Reputation: 9072

This works with the example report you've provided. It returns both the start and completion date and should handle day-of-month suffixes like 'th', 'rd', etc.

import re

import dateutil.parser

REPORT = \
"""Dear Customer,

(Call Transferred) Start Time & Date: 00:05 Monday 6th February 2017
Completion Time & Date: 06:00 Monday 6th February 2017

Details of Work:"""


def parse_report(data):
    dates = []

    for pattern in ['(?<=Start Time & Date: ).*', '(?<=Completion Time & Date: ).*']:
        date = dateutil.parser.parse(re.search(pattern, data).group(0))
        dates.append(date)

    return dates


if __name__ == '__main__':
    start, completion = parse_report(REPORT)
    print('Started: {}, Completed: {}'.format(start, completion))

Output

Started: 2017-02-06 00:05:00, Completed: 2017-02-06 06:00:00

Edit

Updated to use dateutil.parser instead which simplifies the code (thanks to asongtoruin for the suggestion).

Upvotes: 2

Related Questions