简体   繁体   中英

change datetime format in python list

I want to convert date time from str like 2008-1-12 00:00:00 to 20080112 , the datetimes from an excel column. My program is as follows:

def btk_datetime2cvh(table, title):
    datetimes = list(table[title])
    dmyhms_pattern = re.compile(r"^((?:19|20)\d\d)-(0?[1-9]|1[0-2])-(0?[1-9]|[12][0-9]|3[01])\s+(20|21|22|23|[0-1]\d:[0-5]\d:\[0-5]\d)$")

    for num, dt in enumerate(datetimes):
        try:
            year, month, day, time = dmyhms_pattern.findall(dt)[0]
            if int(month) < 10 and int(day) < 10:
                datetimes[num] = year + "0" + str(int(month)) + "0" + str(int(day))
            elif int(month) < 10 and int(day) > 10:
                datetimes[num] = year + "0" + str(int( month)) + day
            elif int(month) > 10 and int(day) < 10:
                datetimes[num] = year + month + "0" + str(int(day))
            elif int(month) > 10 and int(day) > 10:
                datetimes[num] = year + month + day
        except (ValueError,TypeError,IndexError):
            datetimes[num] = "!" + str(dt)

    table[title] = pd.Series(datetimes)
    return table

The result of program running was very strange, most values were successfully converted to 8-bit characters like 20121231 , but some of them were remained same:

2008-10-11 00:00:00
2009-10-24 00:00:00
20070529
20051211
20060818
2016-10-11 00:00:00
20160503
20170908
20170908
20170908
20170908
20170908
20170908
20170908
20170908
20170908
20170908
20170908
20170908
20170908
20170908
20170906
20170906
20170906
20170906
2015-10-13 00:00:00
20170908

At first, I thought it might be caused by the setting of Excel cell format. However, after verifying the output of the program, there is no problem in String splicing of the program, but it seems that the new string has not been successfully assigned to datetimes[num] . I really do not konw why? who can help me?

You may want to use the defacto dateutil library. https://dateutil.readthedocs.io/en/stable/

You can simply do this:

from dateutil import parser
dt = '2008-1-12 00:00:00'
parser.parse(dt).strftime("%Y%m%d")

>>'20080112'

Frequently rolling your own can be really error prone and complex.

If you look at each of the dates that failed to convert, you'll see a pattern:

2008-10-11 00:00:00
2009-10-24 00:00:00
2016-10-11 00:00:00
2015-10-13 00:00:00

All have a month value of 10 .

Now look at your conditions:

int(month) < 10 and int(day) < 10:
int(month) < 10 and int(day) > 10:
int(month) > 10 and int(day) < 10:
int(month) > 10 and int(day) > 10:

You never account for month or day being exactly 10. None of the conditions are true, so nothing is changed.

Either add an else case to handle these instances, or maybe change some < to <= to handle exactly 10.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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