在Python strptim中向时间添加星期几

2024-09-27 04:26:19 发布

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

我在一个csv文件中有两列,其中一列的时间格式是hh:mm:ss,另一列是星期几,1等于星期一,7等于星期日。没有与数据关联的日期。在

我想把数据合并到一个字段中,在这个字段中时间将与一周中的某一天相关联。在

我有时间可以使用strptime:

time.strptime(fl['journey']['BeginTime'][row], "%H:%M:%S")

其格式如下:

^{pr2}$

与星期几对应的数字存储在以下变量中:

fl['journey']['Day'][row]

感谢任何帮助。在


Tags: 文件csv数据time格式hh时间ss
3条回答

这应该能让你:

from datetime import datetime

datetime.strptime("6 13:45:12", "%w %H:%M:%S").timetuple()
time.struct_time(tm_year=1900, tm_mon=1, tm_mday=1, tm_hour=13, tm_min=45, tm_sec=12, tm_wday=0, tm_yday=1, tm_isdst=-1)

记住在Python中,工作日从0=Sunday开始,一直到6

date_string = "%d %s" % (int(fl['journey']['Day'][row]) % 7 ,
                         fl['journey']['BeginTime'][row])

date = time.strptime(date_string, '%w %H:%M:%S')
date_string = '{0} {1}'.format(fl['journey']['Day'][row] - 1,
                               fl['journey']['BeginTime'][row])

dateobj = datetime.datetime.strptime(date_string, '%w %H:%M:%S')

相关问题 更多 >

    热门问题