如何在Python中将星期一24:10转换为星期二00:10?

2024-03-28 19:55:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道如何转换不正确的时间(24小时格式)和天,如 ^Python中的{}到00:10:00 Tuesday?你知道吗

文件建模如下。你知道吗

Time Monday Tuesday ... Friday

i.e. I have a time column along with columns for all the days which take value 0 (not on that day) or 1 (on that day).

这些字段写在一个CSV文件中,我将把它读入一个数据帧中。你知道吗


Tags: 文件thattimeonhave格式时间column
1条回答
网友
1楼 · 发布于 2024-03-28 19:55:50

请尝试以下代码:

from datetime import datetime, timedelta

def str_to_datetime(str_date):
    if date[:2] == "24":
        arr = str_date.split(' ')
        next_day_week = (datetime.strptime(arr[1], '%A') + timedelta(days=1)).strftime('%A')
        str_date = '00' + arr[0][2:] + " " + next_day_week
    return str_date # if you want to convert the string to datetime  > # datetime.strptime(str_date, '%H:%M:%S %A')

str_date = '24:10:00 Monday'
str_to_datetime(date)
# output: '00:10:00 Tuesday'

相关问题 更多 >