Parse strings into Python datetime objects; extends Python's datetime.strptime() with additional features.
Project description
strpdatetime
A replacement for datetime.datetime.strptime
with super powers. strpdatetime
is a drop-in replacement for datetime.datetime.strptime
that adds a simplified regex-like syntax for finding and extracting date and time information from strings.
Why this package?
A common use case is parsing date/time information from strings, for example filenames with
the date and time embedded in them. The standard library's datetime.datetime.strptime
works
well if the string perfectly matches the format string, but does not work if the string
contains additional characters. For example, datetime.datetime.strptime("IMG_1234_2022_11_20.jpeg", "%Y_%m_%d")
will fail with ValueError: time data 'IMG_1234_2022_11_20.jpeg' does not match format '%Y_%m_%d'
. To use datetime.datetime.strptime
in this case, you would need to first parse the string to remove the extra characters.
Third-party packages such as dateutil and datefinder are more flexible but still fail to find the date in the above example and other common filename date/time formats.
strpdatetime
can find the date in the above example using strpdatetime("IMG_1234_2022_11_20.jpeg", "^IMG_*_%Y_%m_%d")
Installation
pip install strpdatetime
To install from source, clone the repository, pip install poetry
, and run poetry install
.
Source Code
The source code is available on GitHub.
Usage
>>> import datetime
>>> from strpdatetime import strpdatetime
>>> dt = strpdatetime("IMG_1234_2022_11_20.jpeg","^IMG_*_%Y_%m_%d.*")
>>> assert dt == datetime.datetime(2022,11,20)
>>>
Syntax
In addition to the standard strptime
format codes, strpdatetime
supports the following:
- *: Match any number of characters
- ^: Match the beginning of the string
- $: Match the end of the string
- {n}: Match exactly n characters
- {n,}: Match at least n characters
- {n,m}: Match at least n characters and at most m characters
- In addition to
%%
for a literal%
, the following format codes are supported:%^
,%$
,%*
,%|
,%{
,%}
for^
,$
,*
,|
,{
,}
respectively - |: join multiple format codes; each code is tried in order until one matches
- Unlike the standard library, the leading zero is not optional for %d, %m, %H, %I, %M, %S, %j, %U, %W, and %V
- For optional leading zero, use %-d, %-m, %-H, %-I, %-M, %-S, %-j, %-U, %-W, and %-V
Examples
>>> from strpdatetime import strpdatetime
>>> strpdatetime("IMG_1234_2022_11_20.jpg","^IMG_{4}_%Y_%m_%d")
datetime.datetime(2022, 11, 20, 0, 0)
>>> strpdatetime("IMG_1234_2022_11_20.jpg","IMG_*_%Y_%m_%d")
datetime.datetime(2022, 11, 20, 0, 0)
>>> strpdatetime("1234_05_06_2022_11_20","%Y_%m_%d$")
datetime.datetime(2022, 11, 20, 0, 0)
>>> strpdatetime("1234_05_06_2022_11_20","IMG_*_%Y_%m_%d|%Y_%m_%d$")
datetime.datetime(2022, 11, 20, 0, 0)
>>>
Command Line
strpdatetime
includes a very simple command line interface. It can be used to test the regex-like syntax. Use python -m strpdatetime --help
for more information.
python -m strpdatetime "^IMG_*_%Y_%m_%d|IMG{4}_%Y-%m-%d-%H%M%S" *.jpg
IMG1234_2022-11-20-063400.jpg, 2022-11-20T06:34:00
IMG_1234_2022_11_21.jpg, 2022-11-21T00:00:00
time data 'IMG_2132.jpg' does not match format '^IMG_*_%Y_%m_%d|IMG{4}_%Y-%m-%d-%H%M%S'
IMG_2132.jpg,
IMG_5678_2022_11_20.jpg, 2022-11-20T00:00:00
License
To ensure backwards compatibility with the Python standard library, strpdatetime
makes use of original code from the standard library and is thus licensed under the Python Software Foundation License, just as Python itself is.
Contributing
Contributions of all kinds are welcome! Please open an issue or pull request on GitHub.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file strpdatetime-0.3.0.tar.gz
.
File metadata
- Download URL: strpdatetime-0.3.0.tar.gz
- Upload date:
- Size: 17.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 01b95a4841db31503f616b3ba545f0b548841598296b17000ba00fbe11e49264 |
|
MD5 | ae25f8138ba6de835359cf202c7cd526 |
|
BLAKE2b-256 | 19cd67359a49878a92da35aa5fec9d3970a34aa3da44b5e29f0c7032be478bc1 |
File details
Details for the file strpdatetime-0.3.0-py3-none-any.whl
.
File metadata
- Download URL: strpdatetime-0.3.0-py3-none-any.whl
- Upload date:
- Size: 17.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | daf11a50e438b62b47241762bdee8857f1890e7eedfcca595ce1f20ab937e967 |
|
MD5 | 4bd6536e9c9bc7325e3bbf6c3a50ec21 |
|
BLAKE2b-256 | 1e323fdee5137beca0aea2502db4b6343a8a852f1648a92b716245a0aeae631a |