我有以下代码(基于
http://strftime.org/):
try: datetime.datetime.strptime("Apr 14,2016 9",'%b %d,%Y %-I') print "matched date format" except ValueError: print "did NOT match date format"
以上打印:
$python parse_log.py did NOT match date format
但是bash会识别这种日期格式:
$date '+%b %d,%Y %-I' Apr 14,2016 1
我错过了什么?
似乎%-I是问题,因为Python匹配没有%-I部分的日期:
try: datetime.datetime.strptime("Apr 14,2016 ",%Y ') print "matched date format" except ValueError: print "did NOT match date format"
输出:
$python parse_log.py matched date format
我在python 2.6.6上.
我需要匹配的实际模式使用12小时时钟,并且是:
datetime.datetime.strptime("Apr 14,2016 9:59:54",%Y %-I:%M:%s')