在Python中将未知时间戳转换为日期时间

在Python中将未知时间戳转换为日期时间,python,datetime,timestamp,Python,Datetime,Timestamp,我正在从WordPressAPI获取一个时间戳,但通常的方法是失败的 我运行以下命令: print pages[1]['dateCreated'] print datetime.fromtimestamp(pages[1]['dateCreated']) 得到这个: 20100228T09:25:07 Traceback (most recent call last): File "C:\Python26\Lib\SITE-P~1\PYTHON~1\pywin\framework\scri

我正在从WordPressAPI获取一个时间戳,但通常的方法是失败的

我运行以下命令:

print pages[1]['dateCreated']
print datetime.fromtimestamp(pages[1]['dateCreated'])
得到这个:

20100228T09:25:07
Traceback (most recent call last):
  File "C:\Python26\Lib\SITE-P~1\PYTHON~1\pywin\framework\scriptutils.py", line 325, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Documents and Settings\mmorisy\Desktop\My Dropbox\python\betterblogmaster3.py", line 28, in <module>
    print datetime.fromtimestamp(pages[1]['dateCreated'])
AttributeError: DateTime instance has no attribute '__float__'
20100228T09:25:07
回溯(最近一次呼叫最后一次):
RunScript中的文件“C:\Python26\Lib\SITE-P~1\PYTHON~1\pywin\framework\scriptutils.py”,第325行
主目录中的exec codeObject__
文件“C:\Documents and Settings\mmorisy\Desktop\My Dropbox\python\betterblogmaster3.py”,第28行,在
打印datetime.fromtimestamp(第[1]['dateCreated']页)
AttributeError:DateTime实例没有属性“\uuuu float\uuuuu”

有什么建议吗?

假设您已从datetime导入datetime

print datetime.strptime(pages[1]['dateCreated'],'%Y%m%dT%H:%M:%S')

. 你有一个字符串,用解析你的字符串,你会得到
datetime
对象。
exec code对象在u u main__;.\u u dict__;
这是做什么的?如果你要保守密码,我们帮不了你什么忙,是吗?此外,如果您实际使用的是这个,那么您就错过了使用Python的要点。看@silenghost谢谢,这很有帮助@洛特先生,我正努力把噪音挡在外面。main._dict_uu来自一个库,不是我想隐藏的东西,但我不明白我是如何“忽略了使用Python的要点”?下次我将尝试包含更多相关信息。
exec
没有抓住使用Python的重点。它已经是一种动态语言了。使用exec使动态语言变得动态。很大程度上是浪费时间。代码不是“噪音”。@Michael:这与正则表达式无关,datetime有自己的用于字符串格式化/解析的模板语言。