How can I parse multiple (unknown) date formats in python?

前端 未结 4 1590
死守一世寂寞
死守一世寂寞 2020-12-10 12:50

I have a bunch of excel documents I am extracting dates from. I am trying to convert these to a standard format so I can put them in a database. Is there a function I can th

4条回答
  •  孤城傲影
    2020-12-10 13:31

    You can use a regex like r'(\d+)\D(\d+)\D(\d+)' to get the month, day and year in a tuple with the re.findall function.

    then just concatenate the 2-digit years with the number 20 or 19 and use the separator you want to join then back:

    '/'.join(the_list)

    As pointed by Tim:

    To normalize days, just do '{0:0>2}'.format(day) and the same to months.

提交回复
热议问题