本文介绍了如何在Python中将String Datetime转换为时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将此字符串 datetimestring ='Fri,08 Jun 2012 22:40:26 GMT'使用python转换为时间戳。
I want to convert this string datetimestring = 'Fri, 08 Jun 2012 22:40:26 GMT' to timestamp using python.
我尝试了
timestamp = time.mktime(time.strptime(datetimestring, '%a, %d %B %Y %H:%M:%S GMT'))但报告正则表达式错误。
but reports regex error.
推荐答案您正在使用%B ,它对应于完整的月份名称,但只有缩写名称。您应该改用%b :
You're using %B, which corresponds to the full month name, but you only have the abbreviated name. You should use %b instead:
>>> import time >>> datetimestring = 'Fri, 08 Jun 2012 22:40:26 GMT' >>> timestamp = time.mktime(time.strptime(datetimestring, '%a, %d %B %Y %H:%M:%S GMT')) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_strptime.py", line 454, in _strptime_time return _strptime(data_string, format)[0] File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_strptime.py", line 325, in _strptime (data_string, format)) ValueError: time data 'Fri, 08 Jun 2012 22:40:26 GMT' does not match format '%a, %d %B %Y %H:%M:%S GMT' >>> timestamp = time.mktime(time.strptime(datetimestring, '%a, %d %b %Y %H:%M:%S GMT')) >>> timestamp 1339209626.0更多推荐
如何在Python中将String Datetime转换为时间戳?
发布评论