使用与日期/时间格式交织在一起的全局通配符模式分析路径中的日期/时间
datetime-glob的Python项目详细描述
日期时间全局
解析路径中的日期/时间,给定的glob模式与类似于strptime/strftime格式的日期/时间格式交织在一起。
date time.datetime.strptime足以进行简单的日期/时间分析。但是,只要需要处理通配符, 它变得很棘手,您需要使用正则表达式。
我们发现glob模式和strtime格式比正则表达式更易于读写,而且 在此模块中封装了涉及正则表达式的逻辑。
安装
- 创建虚拟环境:
python3 -m venv venv3
- 启动:
source venv3/bin/activate
- 使用pip: 安装datetime glob
pip3 install datetime-glob
用法
要匹配路径:
>>>importdatetime_glob>>>matcher=datetime_glob.Matcher(pattern='/some/path/*%Y-%m-%dT%H-%M-%SZ.jpg')>>>matcher.match(path='/some/path/some-text2016-07-03T21-22-23Z.jpg')datetime_glob.Match(year=2016,month=7,day=3,hour=21,minute=22,second=23,microsecond=None)>>>match.as_datetime()datetime.datetime(2016,7,3,21,22,23)>>>match.as_date()datetime.date(2016,7,3)>>>match.as_time()datetime.time(21,22,23)
如果为同一字段指定两次指令,匹配器将确保该字段具有相同的语义 要匹配的值:
>>>importdatetime_glob>>>matcher=datetime_glob.Matcher(pattern='/some/path/%y/%Y-%m-%d.txt')>>>match=matcher.match(path='/some/path/16/2016-07-03.txt')>>>matchdatetime_glob.Match(year=2016,month=7,day=3,hour=None,minute=None,second=None,microsecond=None)>>>match=matcher.match(path='/some/path/19/2016-07-03.txt')>>>type(match)<class'NoneType'>
您可以在文件系统上遍历模式:
>>>importdatetime_glob>>>formatch,pathindatetime_glob.walk(pattern='/some/path/*%Y/%m/%d/%H-%M-%SZ.jpg'):...dtime=match.as_datetime()...print(dtime,path)2016-03-0412:13:14/some/path/saved-2016/03/04/12-13-14Z.jpg2017-11-2322:23:24/some/path/restored-2017/11/23/22-23-24Z.jpg
要在树上手动迭代,并自己递增地匹配每个路径段:
>>>importdatetime_glob>>>pattern_segments=datetime_glob.parse_pattern(pattern='/some/path/*%Y/%m/%d/%H-%M-%SZ.jpg')>>>match=datetime_glob.Match()>>>match=datetime_glob.match_segment(segment='some',pattern_segment=pattern_segments[0],match=match)>>>matchdatetime_glob.Match(year=None,month=None,day=None,hour=None,minute=None,second=None,microsecond=None)>>>match=datetime_glob.match_segment(segment='path',pattern_segment=pattern_segments[1],match=match)>>>matchdatetime_glob.Match(year=None,month=None,day=None,hour=None,minute=None,second=None,microsecond=None)>>>match=datetime_glob.match_segment(segment='some-text2016',pattern_segment=pattern_segments[2],match=match)>>>matchdatetime_glob.Match(year=2016,month=None,day=None,hour=None,minute=None,second=None,microsecond=None)>>>match=datetime_glob.match_segment(segment='07',pattern_segment=pattern_segments[3],match=match)>>>matchdatetime_glob.Match(year=2016,month=7,day=None,hour=None,minute=None,second=None,microsecond=None)>>>match=datetime_glob.match_segment(segment='03',pattern_segment=pattern_segments[4],match=match)>>>matchdatetime_glob.Match(year=2016,month=7,day=3,hour=None,minute=None,second=None,microsecond=None)>>>match=datetime_glob.match_segment(segment='21-22-23Z.jpg',pattern_segment=pattern_segments[5],match=match)>>>matchdatetime_glob.Match(year=2016,month=7,day=3,hour=21,minute=22,second=23,microsecond=None)
支持的strftime指令
(来自https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior的子集)
Directive | Meaning | Example |
---|---|---|
%d | Day of the month as a zero-padded decimal number. | 01, 02, …, 31 |
%-d | Day of the month as a decimal number. | 1, 2, …, 31 |
%m | Month as a zero-padded decimal number. | 01, 02, …, 12 |
%-m | Month as a decimal number. | 1, 2, …, 12 |
%y | Year without century as a zero-padded decimal number. | 00, 01, …, 99 |
%Y | Year with century as a decimal number. | 1970, 1988, 2001, 2013 |
%H | Hour (24-hour clock) as a zero-padded decimal number. | 00, 01, …, 23 |
%-H | Hour (24-hour clock) as a decimal number. | 0, 1, …, 23 |
%M | Minute as a zero-padded decimal number. | 00, 01, …, 59 |
%-M | Minute as a decimal number. | 0, 1, …, 59 |
%S | Second as a zero-padded decimal number. | 00, 01, …, 59 |
%-S | Second as a decimal number. | 0, 1, …, 59 |
%f | Microsecond as a decimal number, zero-padded on the left. | 000000, 000001, …, 999999 |
%% | A literal ‘%’ character. | % |
开发
- 查看存储库。
- 在存储库根目录中,创建虚拟环境:
python3 -m venv venv3
- 激活虚拟环境:
source venv3/bin/activate
- 安装开发依赖项:
pip3 install -e .[dev]
- 运行pre commit.py以在本地执行预提交检查。