簡體   English   中英

迭代日期時間-TypeError:必須為整數

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

我希望從datetime對象創建一系列紀元時間戳。

我可以這樣設置一個日期時間:

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

返回:

1443571200

我希望遍歷日期時間列表以創建一系列時代時間戳記。

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

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

但是,這將返回TypeError: an integer is required

如何迭代使用calendardatetime時間模塊創建紀元時間戳?

您可以使用以下列表推導 只需解壓縮項目並將其傳遞給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