首页 > 解决方案 > datetime.strptime 的替代方法,适用于重复的格式元素

问题描述

我正在尝试按照任意文件模式格式提取查找提取日期时间。其中一些模式包括重复的日期格式元素,例如%Y%M%d.

datetime.datetime.strptime通常对此非常方便,但其底层正则表达式实现排除了重复日期格式元素的使用。

例如,运行以下代码:

import datetime

filepath = '/backups/20190905/data-20190905-230001.tgz'
filepattern = '/backups/%Y%m%d/data-%Y%m%d-%H%M%S.tgz'

backup_time_stamp = datetime.datetime.strptime(filepath, filepattern)

产生以下错误:

Traceback (most recent call last):
  File "strp.py", line 11, in <module>
    backup_time_stamp = datetime.datetime.strptime(filepath, filepattern)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_strptime.py", line 565, in _strptime_datetime
    tt, fraction = _strptime(data_string, format)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_strptime.py", line 345, in _strptime
    format_regex = _TimeRE_cache.compile(format)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_strptime.py", line 275, in compile
    return re_compile(self.pattern(format), IGNORECASE)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/re.py", line 233, in compile
    return _compile(pattern, flags)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/re.py", line 301, in _compile
    p = sre_compile.compile(pattern, flags)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/sre_compile.py", line 562, in compile
    p = sre_parse.parse(p, flags)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/sre_parse.py", line 856, in parse
    p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, False)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/sre_parse.py", line 415, in _parse_sub
    itemsappend(_parse(source, state, verbose))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/sre_parse.py", line 757, in _parse
    raise source.error(err.msg, len(name) + 1) from None
sre_constants.error: redefinition of group name 'Y' as group 4; was group 1 at position 101

这是记录在案datetime.datetime.strptime. 我想知道可能的解决方法是什么。

标签: pythondatetime

解决方案


我建议拆分文件路径以仅从文件名中提取日期时间

import datetime

filepath = '/backups/20190905/data-20190905-230001.tgz'
filename = filepath.split('/')[-1]
filepattern = 'data-%Y%m%d-%H%M%S.tgz'

backup_time_stamp = datetime.datetime.strptime(filename, filepattern)

另一种方法是使用由marko开发的库datetime-glob来从使用与日期/时间格式交织的 glob 通配符模式的路径中解析日期/时间。


推荐阅读