字符串到十进制秒的时间 [英] string to time with decimal seconds

查看:47
本文介绍了字符串到十进制秒的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个格式为 HHMMSS.SS 的字符串,我该如何将其转换为时间对象?

我以为你会这样做:

导入时间time.strptime(timestring, '%H%M%S')

然而,%S 根据 时间 文档.

解决方案

你将不得不使用 %f

time.strptime('26/01/12 23:50:32.123', '%d/%m/%y %H:%M:%S.%f')

使用日期时间是正确的

<预><代码>>>>从日期时间导入日期时间>>>a = datetime.strptime('26/01/12 23:50:32.123', '%d/%m/%y %H:%M:%S.%f')>>>a.微秒

Say I have a string with format HHMMSS.SS how do I convert this to a time object?

This is how I thought you would do it:

import time
time.strptime(timestring, '%H%M%S')

However%S does not take take into account fractions of seconds according to the time documentation.

解决方案

You will have to use %f

time.strptime('26/01/12 23:50:32.123', '%d/%m/%y %H:%M:%S.%f')

Use of datetime will be right

>>> from datetime import datetime
>>> a = datetime.strptime('26/01/12 23:50:32.123', '%d/%m/%y %H:%M:%S.%f')
>>> a.microsecond

这篇关于字符串到十进制秒的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆