0

我正在尝试将以下日期(这是一个字符串)转换为日期时间对象:

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

所以我所做的是抛出 +0000 值,然后使用以下代码将其转换为日期时间对象:

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'

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

4

1 回答 1

1

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

尝试:

published = time.strptime(str(published),"%a, %d %b %Y %H:%M:%S")
于 2013-05-13T15:04:18.997 回答