Python - Find date from string

2024/10/14 5:14:49

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:
Answer

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 reimport dateutil.parserREPORT = \
"""Dear Customer,(Call Transferred) Start Time & Date: 00:05 Monday 6th February 2017
Completion Time & Date: 06:00 Monday 6th February 2017Details 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 datesif __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).

https://en.xdnf.cn/q/117993.html

Related Q&A

Get 1st column values on .csv file on python

i am newbie at python programming, i have a .csv file containing mac address and signal strength data from an AP consider my csv data is:i want to get just mac address values which is the 1st row, ref…

how to click mouse over sub menu in selenium?

I want to click invisible htmls sub menu click.*invisible html source<ul class="options"> <li class="_ranking-attr-filter-container _sub-menu-target"> <span>Hide w…

SSL: CERTIFICATE_VERIFY_FAILED certificate verify failed (_ssl.c.661)

Im trying to install nltk on my Mac, but I keep getting this error message after following these instructions: Install NLTK: sudo pip install -U nltk Install Numpy (optional): sudo pip install -U numpy…

Real-time reading of terminal output from server

Im trying to process images from my camera on my server and get the information after processing on my local machine in real-time. I can get necessary information as terminal outputs on my server, but …

Transform map to mapPartition using pyspark

I am trying to load a tensorflow model from disk and predicting the values.Codedef get_value(row):print("**********************************************")graph = tf.Graph()rowkey = row[0]check…

Module google_auth_httplib2 not found after pip installing google-cloud How can I fix it?

I used pip to install cloud-storage, like this:$ pip install --upgrade google-cloudWhen I started my application, I got an error that said no module named google_auth_httplib2 was found. I used pip lis…

python unbinding/disable key binding after click and resume it later

Im trying to unbind/disable key once its clicked, and resume its function after 2s. But I cant figure out the code for the unbinding. The bind is on window. Heres the code that I tried so far:self.choi…

Extracting information from pandas dataframe

I have the below dataframe. I want to build a rule engine to extract the tokens where the pattern is like Eg. "UNITED STATES" .What is the best way to do it ? Is there anything like regex o…

scipy import error with pyinstaller

I am trying to build a "One File" executable for my project with pyinstaller and a .spec file. The content of the spec file is as follows:# -*- mode: python -*-block_cipher = Nonea = Analysi…

How to compare meaningful level of a set of phrase that describe same concept in NLP?

I have two terms "vehicle" and "motor vehicle". Are there any way to compare the meaningfulness level or ambiguity level of these two in NLP? The outcome should be that "motor…