将时间转换为纪元(Python)

时间:2017-01-17 14:43:47

标签: python python-2.7 datetime tweepy epoch

我使用tweepy来发推文。我试图通过Slack发送这条推文。 Tweepy以奇怪的格式给出时间,而Slack需要时间段。

for i in tweets:
    created=(i.created_at)
    print(created)
    print(created.strftime('%s'))

返回

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

如何将2017-01-17 14:36:26送入纪元?

2 个答案:

答案 0 :(得分:5)

你必须将字符串转换为时间元组 - 通过适当的规范,然后将时间元组转换为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)

结果是 1484656586.0

答案 1 :(得分:0)

我不知道<dependency> <groupId>com.wonderland</groupId> <artifactId>project-a</artifactId> <version>1.0</version> <exclusions> <exclusion> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> </exclusion> <!-- ... everything else to be excluded --> <exclusions> </dependency> 的类型,但这应该有效:

created
相关问题