你知道出了什么问题吗?
import random
import datetime
import csv
from itertools import groupby
def generator():
i=0
while 1:
yield random.randint(-1, 1), datetime.datetime.now()
i=i+1
def keyfunc(timestamp,interval):
xt = datetime.datetime(2013, 4,4)
dt=timestamp
delta_second =(dt - xt).seconds
normalize_second = (delta_second / (interval*60)) * (interval*60)
newtime = xt + timedelta(seconds=normalize_second)
return newtime
mynumber = 100
for random_number, current_time in generator():
mynumber += random_number
reftime5min = keyfunc(current_time,5)
print mynumber,",", current_time, reftime5min
我现在得到的错误是:
Traceback (most recent call last): File "", line 35, in File "", line 28, in keyfunc NameError: global name 'timedelta' is not defined
答案 0 :(得分:4)
将timedelta
更改为datetime.timedelta
。您没有直接导入timedelta
类,因此您需要使用限定名称。
答案 1 :(得分:1)
from datetime import timedelta