将时间转换为纪元(Python)

use*_*110 5 python datetime epoch tweepy python-2.7

我用tweepy来发推文.我试图通过Slack发送这条推文.Tweepy以奇怪的格式给出时间,Slack需要时间在epoch中.

for i in tweets:
    created=(i.created_at)
    print(created)
    print(created.strftime('%s'))
Run Code Online (Sandbox Code Playgroud)

这回来了

2017-01-17 14:36:26

Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\slackbot3\run.py", line 287, in main
print(created.strftime('%s'))
ValueError: Invalid format string
Run Code Online (Sandbox Code Playgroud)

怎么能把2017-01-17 14:36:26送到纪元时间?

vol*_*ano 6

您必须将字符串转换为时间元组 - 通过适当的规范,然后将时间元组转换为EPOCH时间.在Python中,它有点尴尬

import time
time_tuple = time.strptime('2017-01-17 14:36:26', '%Y-%m-%d %H:%M:%S')
time_epoch = time.mktime(time_tuple)
Run Code Online (Sandbox Code Playgroud)

结果是1484656586.0