Can I use regexes within datetime.strptime formats?

2024/9/8 8:52:50

I have string values that contain a trailing time stamp. I thought I could use strptime with a regex pattern to extract those.

Like:

from __future__ import print_functionfrom datetime import datetime
# this here works
input_with_ts = "20170410_1133"
print(datetime.strptime(input_with_ts, '%Y%m%d_%H%M'))
# but this is how things really look like
input_with_ts = "foo_bar_D31_848_20170410_1133"
print(datetime.strptime(input_with_ts, 'foo_bar_.*_.*_%Y%m%d_%H%M'))

Gives:

2017-04-10 11:33:00
Traceback (most recent call last):File "test.py", line 9, in <module>print(datetime.strptime(input_with_ts, 'foo_bar_.*_.*_%Y%m%d_%H%M'))File "/usr/lib/python2.7/_strptime.py", line 332, in _strptime
(data_string, format))
ValueError: time data 'foo_bar_D31_848_20170410_1133' does not match format 'foo_bar_.*_.*_%Y%m%d_%H%M'

Simply wondering: is that even possible - putting a regex pattern into that format string? If not, what is the straight canonical way to get me there?

Answer

No, you can't, only fixed text (so literals) and date-time components are supported.

Just extract the datetime portion first; you can use a regex for that task of course. Not that that is needed in your example, because the datetime portion is a fixed-width chunk of text at the end:

datetime.strptime(input_with_ts[-13:], '%Y%m%d_%H%M')
https://en.xdnf.cn/q/72990.html

Related Q&A

Sort a list with longest items first

I am using a lambda to modify the behaviour of sort.sorted(list, key=lambda item:(item.lower(),len(item)))Sorting a list containing the elements A1,A2,A3,A,B1,B2,B3,B, the result is A,A1,A2,A3,B,B1,B2,…

Multivariate Root Finding in Python

Using excel solver, it is easy to find a solution (optimum value for x and y )for this equation: (x*14.80461) + (y * -4.9233) + (10*0.4803) ≈ 0However, I cant figure out how to do this in Python. The …

Python print not working when embedded into MPI program

I have an Python 3 interpreter embedded into an C++ MPI application. This application loads a script and passes it to the interpreter.When I execute the program on 1 process without the MPI launcher (s…

Detecting USB Device Insertion on Windows 10 using python

I cant get the following code for Detecting USB Device Insertion to work on my Windows 10 (64 bit) computer with Python 3.7.import win32serviceutil import win32service import win32event import servicem…

I get NotImplementedError when trying to do a prepared statement with mysql python connector

I want to use prepared statements to insert data into a MySQL DB (version 5.7) using python, but I keep getting a NotImplementedError. Im following the documentation here: https://dev.mysql.com/doc/con…

parsing transcript .srt files into readable text

I have a video transcript SRT file with lines in conventional SRT format. Heres an example:1 00:00:00,710 --> 00:00:03,220 Lorem ipsum dolor sit amet consectetur, adipisicing elit.2 00:00:03,220 --…

Sphinx Public API documentation

I have a large number of python file and I would like to generate public API documentation for my project. All the functions that are part of the api I have decorated with a decorator. for example:@api…

Debugging a scripting language like ruby

I am basically from the world of C language programming, now delving into the world of scripting languages like Ruby and Python.I am wondering how to do debugging. At present the steps I follow is, I c…

Running unittest Test Cases and Robot Framework Test Cases Together

Our group is evaluating Robot Test Framework for our QA group, not just for BDD, but also to possibly cover a lot of our regular functionality testing needs. It certainly is a compelling project.To wha…

numpy.ndarray enumeration over a proper subset of the dimensions?

(In this post, let np be shorthand for numpy.)Suppose a is a (n + k)&#x2011;dimensional np.ndarray object, for some integers n > 1 and k > 1. (IOW, n + k > 3 is the value of a.ndim). I w…