简体   繁体   English

迭代日期时间-TypeError:必须为整数

[英]Iterative Datetime - TypeError: an integer is required

I wish to create a series of epoch time stamps from a datetime object. 我希望从datetime对象创建一系列纪元时间戳。

I can do this with one datetime like so: 我可以这样设置一个日期时间:

start_time = calendar.timegm(datetime.datetime(2015,9,30,0).timetuple())

returning: 返回:

1443571200

I wish to iterate over a list of datetimes to create a series of epoch time stamps. 我希望遍历日期时间列表以创建一系列时代时间戳记。

slices = [(2015,9,30,0),(2015,10,04,23)]

for time_slice in slices:
    start_time = calendar.timegm(datetime.datetime(time_slice).timetuple())

However, this returns a TypeError: an integer is required 但是,这将返回TypeError: an integer is required

How do I iteratively use the calendar and datetime modules to create epoch time stamps? 如何迭代使用calendardatetime时间模块创建纪元时间戳?

You can use the following list comprehension . 您可以使用以下列表推导 Simply unpack the item and pass it to the datetime constructor: 只需解压缩项目并将其传递给datetime构造函数即可:

>>> from calendar import timegm
>>> from datetime import datetime
>>> start_times  = [timegm(datetime(*slc).timetuple()) for slc in slices] 
>>> start_times
[1443571200, 1443999600]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM