time.strptime()不遵守格式规定
虽然我通过"%a %b %d %H:%M:%S %Z %Y"
作为time.strptime()
中的格式字符串,但它在'%a %b %d %H:%M:%S %Y'
上运行,因此导致错误。任何想法可能会导致它?time.strptime()不遵守格式规定
同样的事情在python控制台中运行完美,但在实际的代码中没有。
Exception in thread Thread-7:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "proxy.py", line 137, in listenThread
response = self.fetchRequest(raw_request, request)
File "proxy.py", line 114, in fetchRequest
if request['type'] == "GET" and self.is_cachable(request, response_headers):
File "proxy.py", line 100, in is_cachable
requestTime = time.mktime(time.strptime(self.request_log[request['url']][len(self.request_log[request['url']])-3]), "%a %b %d %H:%M:%S %Z %Y")
File "/usr/lib/python2.7/_strptime.py", line 478, in _strptime_time
return _strptime(data_string, format)[0]
File "/usr/lib/python2.7/_strptime.py", line 332, in _strptime
(data_string, format))
ValueError: time data 'Mon Apr 10 22:52:38 IST 2017' does not match format '%a %b %d %H:%M:%S %Y'
Actualy我刚刚作出了一个非常愚蠢的错误
requestTime = time.mktime(time.strptime(self.request_log[request['url']][len(self.request_log[request['url']])-3]), "%a %b %d %H:%M:%S %Z %Y")
括号的顺序是错误的上面,它应该是这样的
requestTime = time.mktime(time.strptime(self.request_log[request['url']][len(self.request_log[request['url']])-3], "%a %b %d %H:%M:%S %Z %Y"))
我其实是只传递时间字符串时间.strptime()离开格式
对不起所引起的问题
啊,的确,这是*默认格式.. –
%Z的价值来源于附着在datetime
对象tzinfo
对象,但没有datetime
对象(因此没有tzinfo
要么)如果你使用strptime()
代替strftime()
。事实上,标准Python库附带的唯一tzinfo
对象是固定偏移量timezone
对象,然后仅在Python 3中。除标准UTC值外,标准库没有这些的标准列表;相反,您需要根据需要创建它们,或者使用pytz。因此,Python标准库没有将任意三字母代码翻译成时区的机制。它应该能够处理'UTC'
,一些同义词,也许你的本地时间是由系统区域设置返回的,但这就是它。
(提示:对于大多数人来说,pytz是正确的事情使用。)
三个字母组成的代码是notgloballyunique无论如何,所以不存在“正确”的方式做你问什么。您需要具有类似Olson database标识符的内容(例如America/New_York
大致与东部标准/日光时间同义)。
即使对于'%Z',回溯也应该显示传入的格式。这里不是这种情况。我可以得出的唯一结论是*服务器正在运行陈旧的字节码*。源代码更新。 –
@MartijnPieters:如果OP的源代码与OP的字节码相匹配,那将会很不错,但不幸的是,这不会解决OP的实际问题,那就是他们正在尝试做一些不可能的事情。 – Kevin
当然,但*这是错误是关于*。使用的实际格式不包含'%Z'。 –
请显示导致问题的实际代码。 – DyZ
您在“Apr”和“10”之间获得了额外的空间。 –
您必须运行的代码不是您认为的自己。 Python不会随机使用不同的硬编码字符串。 –