我很难过。我在Mac上编写了Python 2.7.5中的日期清理函数,但在我的Ubuntu服务器上没有2.7.6。Python的日期时间strptime()机器之间不一致
Python 2.7.5 (default, Mar 9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> date = datetime.strptime('2013-08-15 10:23:05 PDT', '%Y-%m-%d %H:%M:%S %Z')
>>> print(date)
2013-08-15 10:23:05
为什么在Ubuntu的2.7.6上这不起作用?
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> date = datetime.strptime('2013-08-15 10:23:05 PDT', '%Y-%m-%d %H:%M:%S %Z')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/_strptime.py", line 325, in _strptime
(data_string, format))
ValueError: time data '2013-08-15 10:23:05 PDT' does not match format '%Y-%m-%d %H:%M:%S %Z'
编辑:我尝试使用时区以小写%Z偏移,但仍得到一个错误(虽然不同的一个):
>>> date = datetime.strptime('2013-08-15 10:23:05 -0700', '%Y-%m-%d %H:%M:%S %z')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_strptime.py", line 317, in _strptime
(bad_directive, format))
ValueError: 'z' is a bad directive in format '%Y-%m-%d %H:%M:%S %z'
只是将它缩小,同一台机器或不同机器吗? – NPE 2014-09-10 20:01:44
哦,只是注意到不同的操作系统,如此明显不同的机器。 – NPE 2014-09-10 20:02:18
不同的机器。我在我的Mac上有2.7.5,但我试图运行脚本的服务器(Ubuntu)有2.7.6。 – 2014-09-10 20:02:48