python - 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
. 我想知道可能的解决方法是什么。
解决方案
我建议拆分文件路径以仅从文件名中提取日期时间
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 通配符模式的路径中解析日期/时间。
推荐阅读
- haskell - 将`lookup`返回的`Nothing`映射到默认值
- json - 从 json 文件中提取属性段
- serialization - /registration/api/registration/ 名称错误未定义名称“名称”
- c++ - 读取运行时知道,从二进制文件堆分配对象
- discord - 是否可以从不和谐的机器人中打印出 IndexError?
- pyiron - PyIron 中直接包含 lammps 命令
- python-3.x - 为 IDLE 设置 Python 环境变量?
- java - Spring security ignore url 不适用于 we security ignore 方法
- javascript - 如何在 React 中正确使用 async/await 和 Array.filter?
- android - Preference.setLayoutResource(R.layout.my_custom_layout) 不接受 OnClickListener