简体   繁体   English

如果您不知道日期输入的格式,如何格式化日期字符串?

[英]How to format a date string if you do not know the format of date input?

I have to read through a ton of files based on their file name. 我必须根据文件名读取大量文件。 They can vary from the format YMD, YMD, M_D_Y, or Y_M_D. 它们可以与YMD,YMD,M_D_Y或Y_M_D格式不同。 There could be others but at the moment this is all I am given to work with. 可能还有其他人,但目前这是我得到的所有工作。

I need to be able to extract the dates, which I have already done using a regular expression, and format them into the form YMD. 我需要能够使用正则表达式提取日期,并将它们格式化为YMD格式。 For example if my input string is 06_12_2018, I need to be able to format that into 20180612 so I can do comparisons with another file later. 例如,如果我的输入字符串是06_12_2018,我需要能够将其格式化为20180612,以便稍后我可以与另一个文件进行比较。

What I have tried so far: 到目前为止我尝试了什么:

def cleanDate(date):
    datePatterns = [“%Y%m%d”, “%Y_%m_%d”, “%Y-%m-%d”, “%m_%d_%Y”]
    for pattern in datePatterns:
        if date in datePatterns:
            return datetime.strftime(date, “%Y%m%d”)
        else:
            print “String format not found!”
            return

Now that I am looking at it, it does not make sense to do if date in datePatterns . 现在我正在看它, if date in datePatternsif date in datePatterns是没有意义的。 What is the best way to approach this? 解决这个问题的最佳方法是什么?

The best way will be to use try/except: 最好的方法是使用try / except:

for pattern in datePatterns:
    try:
        return datetime.strptime(date, pattern)
    except ValueError:
        pass
else:
    # none of the datePatterns worked
    raise Exception('failed to parse')

Note that it is strptime you want here, not strftime . 需要注意的是strptime你想在这里,没有strftime Reminder for the wetware: p is for p arsing, f is for f ormatting. 提醒的湿件:p是对于p arsing,f是对于f ormatting。

They can vary from the format YMD, YMD, M_D_Y, or Y_M_D. 它们可以与YMD,YMD,M_D_Y或Y_M_D格式不同。 There could be others but at the moment this is all I am given to work with. 可能还有其他人,但目前这是我得到的所有工作。

If there could be other formats, consider to use dateutil.parser instead, which uses heuristics to guess the format. 如果可能有其他格式,请考虑使用dateutil.parser ,它使用启发式方法来猜测格式。 It's fairly popular, battle-tested and reliable. 它相当受欢迎,经过实战考验且可靠。

>>> from dateutil.parser import parse  # pip install python-dateutil
>>> parse("2018-05-12")
datetime.datetime(2018, 5, 12, 0, 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM