2013-05-13 36 views
0

我试图在以下日期(这是一个字符串)转换为datetime对象:格式与time.strptime()抛出一个错误

Fri, 26 Apr 2013 12:00:00 +0000 

所以我所做的就是扔+0000值出来,然后用下面的代码将其转换为一个DateTime对象:

published = entry['published'] 
print published 
published = published[:-6] 
print published 
published = time.strptime("%a, %d %b %Y %H:%M:%S",str(published)) 

,然后抛出一个异常,说给定的值是不正确的格式

Fri, 26 Apr 2013 12:00:00 +0000 
Fri, 26 Apr 2013 12:00:00 
     C:\Python27\python.exe 
    C:/obfuscated.py 
     Traceback (most recent call last): 
     Fri, 26 Apr 2013 12:00:00 +0000 
      File "C:/obfuscated.py", line 17, in <module> 
     Fri, 26 Apr 2013 12:00:00 
      class MyClass(object): 
      File "C:/obfuscated.py", line 38, in MyClass 
      published = time.strptime("%a, %d %b %Y %H:%M:%S",str(published)) 
      File "C:\Python27\lib\_strptime.py", line 467, in _strptime_time 
      return _strptime(data_string, format)[0] 
      File "C:\Python27\lib\_strptime.py", line 325, in _strptime 
      (data_string, format)) 
     ValueError: time data '%a, %d %b %Y %H:%M:%S' does not match format 'Fri, 26 Apr 2013 12:00:00' 

我不确定为什么这会失败,因为日期时间字符串格式对我来说似乎是正确的。

+0

阅读[文件](http://docs.python.org/2/library/time.html#time.strptime)!错误信息其实很清楚(时间数据'xxx'与格式'yyy'不匹配) – 2013-05-13 15:04:10

+0

是的,愚蠢的错误>< – 2013-05-13 15:07:12

回答

1

你的参数是错误的,格式字符串应该是第二个参数。

尝试:

published = time.strptime(str(published),"%a, %d %b %Y %H:%M:%S") 
+0

哇,愚蠢的错误><谢谢! – 2013-05-13 15:06:22

+0

不用担心。如果它解决了您的问题,您可能需要考虑接受我的答案。谢谢 – stonesam92 2013-05-13 15:09:46