简体   繁体   English

Python 3-使用datetime.time对象-timedelta

[英]Python 3 - Working with datetime.time objects - timedelta

Just got a problem I don't understand in my python code. 刚遇到一个我在python代码中无法理解的问题。

timestamps = list(map(lambda ts: ts.time, timestamps))
start_timestamps = timestamps
end_timestamps = timestamps[1:]
d = date(2000, 1, 1)
FMT = '%H:%M:%S'
for i, t1 in enumerate(start_timestamps):
    t1 = datetime.combine(d, start_timestamps[i])
    t2 = datetime.combine(d, end_timestamps[i])
    dt = t1 - t2
    #dt = datetime.strptime(t1,FMT) - datetime.strptime(t2,FMT)

By using this in my django project I get the following error: 通过在我的django项目中使用它,我得到以下错误:

t2 = datetime.combine(d, end_timestamps[i])

TypeError: combine() argument 2 must be datetime.time, not bytes TypeError:Combine()参数2必须为datetime.time,而不是字节

When uncommenting the last line und commenting the three lines above it, I'm getting the following: 当取消注释最后一行并注释其上方的三行时,我得到以下信息:

dt = datetime.strptime(t1,FMT) - datetime.strptime(t2,FMT)

TypeError: must be str, not datetime.time TypeError:必须为str,而不是datetime.time

So I really don't understand it. 所以我真的不明白。 It is datetime.time , but the same time it is not ... Can anyone help me? 它是datetime.time ,但不是同一时间。有人可以帮助我吗?

Thanks! 谢谢!

Some of your objects are bytes objects, and others are datetime.time objects. 您的某些对象是bytes对象,而另一些是datetime.time对象。 In other words, you have a mix of objects. 换句话说,您有多种对象。

Note that it was end_timestamps[i] that failed, not start_timestamps[i] , in your first error. 请注意,您的第一个错误是end_timestamps[i]失败,而不是start_timestamps[i] Ergo, start_timestamps[i] was a time object. 因此, start_timestamps[i]是一个time对象。 If all objects in timestamps where bytes objects, datetime.combine(d, start_timestamps[i]) would have failed. 如果timestamps中的所有对象都包含bytes对象,则datetime.combine(d, start_timestamps[i])将会失败。

You need to figure out why your ts.time() calls in your map() call return inconsistent data types. 你需要弄清楚为什么你的ts.time()在您的调用map()调用返回不一致的数据类型。

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

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