2010-11-03 7 views
2

Je reçois un horodatage de l'API WordPress, mais la méthode habituelle de converting from timestamp to datetime ne fonctionne pas.Conversion d'un horodatage inconnu en datetime en Python

Je cours ceci:

print pages[1]['dateCreated'] 
print datetime.fromtimestamp(pages[1]['dateCreated']) 

Et obtenez ceci:

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__' 

Toutes les suggestions?

+2

[ 'timestamp' censé être un flotteur] (http://docs.python.org/library/datetime.html#datetime.date.fromtimestamp). Vous avez une chaîne, analysez votre chaîne avec ['datetime.strptime'] (http://docs.python.org/library/datetime.html#datetime.datetime.strptime) et vous obtiendrez l'objet' datetime'. – SilentGhost

+1

'exec codeObject dans __main __.__ dict__' Qu'est-ce que cela fait? Si vous voulez garder votre code secret, nous ne pouvons rien faire, n'est-ce pas? En outre, si vous utilisez réellement ceci, vous manquez le point d'utiliser Python. –

+1

Ce que vous avez là est un horodatage ISO 8601, pas un horodatage Unix. Voir http://stackoverflow.com/questions/969285/how-do-i-translate-a-iso-8601-datetime-string-into-a-python-datetime-object –

Répondre

3

supposerons que vous avez from datetime import datetime:

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

@Michael: cela n'a rien à voir avec regex, le datetime a son propre langage de template pour le formatage/l'analyse de chaîne. – SilentGhost

+0

Merci à vous deux. –