使用与日期/时间格式交织在一起的全局通配符模式分析路径中的日期/时间

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的子集)

DirectiveMeaningExample
%dDay of the month as a zero-padded decimal number.01, 02, …, 31
%-dDay of the month as a decimal number.1, 2, …, 31
%mMonth as a zero-padded decimal number.01, 02, …, 12
%-mMonth as a decimal number.1, 2, …, 12
%yYear without century as a zero-padded decimal number.00, 01, …, 99
%YYear with century as a decimal number.1970, 1988, 2001, 2013
%HHour (24-hour clock) as a zero-padded decimal number.00, 01, …, 23
%-HHour (24-hour clock) as a decimal number.0, 1, …, 23
%MMinute as a zero-padded decimal number.00, 01, …, 59
%-MMinute as a decimal number.0, 1, …, 59
%SSecond as a zero-padded decimal number.00, 01, …, 59
%-SSecond as a decimal number.0, 1, …, 59
%fMicrosecond 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以在本地执行预提交检查。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java对称二维随机数数组   java Spring 3基于使用另一个数据源查询的连接字符串设置web应用程序数据源   java如何将类型保存为变量并在cast中使用   使用Struts的Jsp页面中出现java重复前缀错误   如何在SpringJava中自定义排序?   java如何高效地从自定义对象实现JList?   java如何在我的Android应用程序中调用外部库项目   java如何合并JTable的两列   java如何从字符串中提取括号数据   java Click recyclerView新建活动错误尝试调用虚拟方法   如何用Java表示字符串列表和列表?   swing java。awt。机器人按键事件为继续按下   java安全地忽略包含所有其他对象的JSON属性   java合并自定义双链表