我正在尝试借助此代码从 Python 中的字符串中解析多个日期,
from dateutil.parser import _timelex, parser
a = "Approve my leave from first half of 12/10/2012 to second half of 20/10/2012 "
p = parser()
info = p.info
def timetoken(token):
try:
float(token)
return True
except ValueError:
pass
return any(f(token) for f in (info.jump,info.weekday,info.month,info.hms,info.ampm,info.pertain,info.utczone,info.tzoffset))
def timesplit(input_string):
batch = []
for token in _timelex(input_string):
if timetoken(token):
if info.jump(token):
continue
batch.append(token)
else:
if batch:
yield " ".join(batch)
batch = []
if batch:
yield " ".join(batch)
for item in timesplit(a):
print "Found:", item
print "Parsed:", p.parse(item)
并且代码将字符串的后半部分作为第二个日期并给我这个错误,
raise ValueError, "unknown string format"
ValueError: unknown string format
当我将“后半部分”更改为“后半部分”或“前半部分”时,一切正常。
谁能帮我解析这个字符串?