Twitter created_at在python中转换纪元时间

Yas*_*mur 1 python twitter

我在Twitter上有这个日期:

created_at = "Wed Aug 29 17:12:58 +0000 2012"
Run Code Online (Sandbox Code Playgroud)

我想使用以下内容将其转换为时间:

time.mktime(created_at)
Run Code Online (Sandbox Code Playgroud)

但我得到这个错误:

TypeError: argument must be 9-item sequence, not str
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

小智 6

在使用它strptime之前,您需要将传入的字符串转换为Python时间元组.

此代码将获取输入字符串,将其转换为元组,然后使用以下命令将其转换为Unix-epoch time float time.mktime:

import time
created_at = "Wed Aug 29 17:12:58 +0000 2012"
print time.mktime(time.strptime(created_at,"%a %b %d %H:%M:%S +0000 %Y"))
Run Code Online (Sandbox Code Playgroud)