python如何将字符串转为时间,Python:如何将字符串转换为日期时间

I am parsing an XML file that gives me the time in the respective isoformat:

tc1 = 2012-09-28T16:41:12.9976565

tc2 = 2012-09-28T23:57:44.6636597

But it is being treated as a string when I retrieve this from the XML file.

I have two such time values and i need to do a diff between the two so as to find delta.

But since it is a string I can not directly do tc2-tc1. But since they are already in isoformat for datetime, how do i get python to recognize it as datetime?

thanks.

解决方案import datetime

datetime.datetime.strptime(your_string, "%Y-%m-%dT%H:%M:%S.%f")

The link provided presents the different format directives. Note that the microseconds are limited to the range [0,999999], meaning that a ValueError will be raised with your example (you're using 1/10us): you need to truncate your string to drop the final character.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值