gpt4 book ai didi

python - datetime.strptime 的替代方案,适用于重复的格式元素

转载 作者:行者123 更新时间:2023-12-01 07:07:15 25 4
gpt4 key购买 nike

我正在尝试按照任意文件模式格式提取查找提取日期时间。其中一些模式包括重复的日期格式元素,例如 %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

这是一个documented 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)

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

关于python - datetime.strptime 的替代方案,适用于重复的格式元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58388089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com