How to determine appropriate strftime format from a date string?

后端 未结 4 1064
误落风尘
误落风尘 2020-12-09 08:33

The dateutil parser does a great job of correctly guessing the date and time from a wide variety of sources.

We are processing files in which each file

4条回答
  •  遥遥无期
    2020-12-09 09:05

    You can write your own parser:

    import datetime
    
    class DateFormatFinder:
        def __init__(self):
            self.fmts = []
    
        def add(self,fmt):
            self.fmts.append(fmt)
    
        def find(self, ss):
            for fmt in self.fmts:            
                try:
                    datetime.datetime.strptime(ss, fmt)
                    return fmt
                except:
                    pass
            return None
    

    You can use it as follows:

    >>> df = DateFormatFinder()
    >>> df.add('%m/%d/%y %H:%M')
    >>> df.add('%m/%d/%y')
    >>> df.add('%H:%M')
    
    >>> df.find("01/02/06 16:30")
    '%m/%d/%y %H:%M'
    >>> df.find("01/02/06")
    '%m/%d/%y'
    >>> df.find("16:30")
    '%H:%M'
    >>> df.find("01/02/06 16:30")
    '%m/%d/%y %H:%M'
    >>> df.find("01/02/2006")
    

    However, It is not so simple as dates can be ambiguous and their format can not be determined without some context.

    >>> datetime.strptime("01/02/06 16:30", "%m/%d/%y %H:%M") # us format
    datetime.datetime(2006, 1, 2, 16, 30)
    >>> datetime.strptime("01/02/06 16:30", "%d/%m/%y %H:%M") # european format
    datetime.datetime(2006, 2, 1, 16, 30)
    

提交回复
热议问题