Python - Getting the date format [duplicate]

落花浮王杯 提交于 2019-12-01 09:38:42

I had a look in the source code and, unfortunately, python dateutil doesn't expose the format. In fact it doesn't even generate a guess for the format at all, it just goes ahead and parses - the code is like a big nested spaghetti of conditionals.

You could have a look at dateinfer which looks to be what you're searching for, but these are unrelated libraries so there is no guarantee at all that python-util will parse with the same format that dateinfer suggests.

>>> from dateinfer import infer
>>> s = "2015-09-01T12:34:15.601+03:00"
>>> infer([s])
'%Y-%d-%mT%I:%M:%S.601+%m:%d'

Look at that .601. Close but not cigar. I think it has probably also mixed up the month and the day. You might get better results by giving it more than one date string to base the guess upon.

jfs

i need this in order to verify that it's in the correct format

If you know the expected time format (or a set of valid time formats) then you could just parse the input using it: if it succeeds then the time format is valid (the usual EAFP approach in Python):

for date_format in valid_date_formats:
    try:
        return datetime.strptime(date_string, date_format), date_format
    except ValueError: # wrong date format
        pass # try the next format
raise ValueError("{date_string} is not in the correct format. "
                 "valid formats: {valid_date_formats}".format(**vars()))

Here's a complete code example (in Russian -- ignore the text, look at the code).

If there are many valid date formats then to improve time performance you might want to combine them into a single regular expression or convert the regex to a deterministic or non-deterministic finite-state automaton (DFA or NFA).

In general, if you need to extract dates from a larger text that is too varied to create parsing rules manually; consider machine learning solutions e.g., a NER system such as webstruct (for html input).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!