幽灵资源网 Design By www.bzswh.com
我正在尝试将字符串“20091229050936”转换为“2009年12月29日(UTC)”
>import time >s = time.strptime("20091229050936", "%Y%m%d%H%M%S") >print s.strftime('%H:%M %d %B %Y (UTC)')
给 AttributeError: 'time.struct_time' object has no attribute 'strftime'
显然,我犯了一个错误:时间错了,它是一个日期时间对象!它有一个日期和时间组件!
>import datetime >s = datetime.strptime("20091229050936", "%Y%m%d%H%M%S")
给 AttributeError: 'module' object has no attribute 'strptime'
我是怎么意思将字符串转换为格式化的日期字符串?
解决方案
time.strptime返回time_struct; time.strftime接受a time_struct作为可选参数:
>s = time.strptime(page.editTime(), "%Y%m%d%H%M%S") >print time.strftime('%H:%M %d %B %Y (UTC)', s) 给 05:09 29 December 2009 (UTC)
总结
以上所述是小编给大家介绍的使用Python将字符串转换为格式化的日期时间字符串,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
幽灵资源网 Design By www.bzswh.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
幽灵资源网 Design By www.bzswh.com
暂无评论...