In [1]: from datetime import datetime
In [2]: def validate(date_string):
try:
datetime.strptime(date_string, '%Y%m%d%f')
print('Valid')
except ValueError:
print('Invalid')
In [3]: validate('2017122512345')
Valid
In [4]: validate('20171')
Invalid
In [5]: validate('illegal')
Invalid
Why go the regular expression route for this kind of a problem?
Easier to ask for forgiveness-尝试使用
datetime.strptime()
加载并处理可能的ValueError
-%Y%m%d%f
格式应该是您要查找的格式:相关问题 更多 >
编程相关推荐