我從WordPress API獲取時間戳,但通常的方法converting from timestamp to datetime正在讓我失望。在Python中將未知時間戳轉換爲日期時間
我運行此:
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__'
有什麼建議?
['timestamp'應該是一個浮動(http://docs.python.org/library/datetime.html#datetime.date.fromtimestamp)。你有一個字符串,用['datetime.strptime']解析你的字符串(http://docs.python.org/library/datetime.html#datetime.datetime.strptime),你會得到'datetime'對象。 – SilentGhost 2010-11-03 15:14:30
'__main ______ dict__'中的exec codeObject這是幹什麼用的?如果你想保密你的代碼,我們不能幫忙,我們可以嗎?此外,如果你真的使用這個,你就錯過了使用Python的點。 – 2010-11-03 15:14:57
你有什麼是ISO 8601時間戳,而不是Unix時間戳。請參閱http://stackoverflow.com/questions/969285/how-do-i-translate-a-iso-8601-datetime-string-into-a-python-datetime-object – 2010-11-03 15:27:06