Python - Getting the date format [duplicate]

半世苍凉 提交于 2019-12-30 10:57:10

问题


I'm getting a date as a string, then I'm parsing it to datetime object. Is there any way to check what's is the date format of the object?

Let's say that this is the object that I'm creating:

modified_date = parser.parse("2015-09-01T12:34:15.601+03:00")

How can i print or get the exact date format of this object, i need this in order to verify that it's in the correct format, so I'll be able to to make a diff of today's date and the given date.


回答1:


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.




回答2:


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).



来源:https://stackoverflow.com/questions/34073502/python-getting-the-date-format

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