简体   繁体   中英

Python 3 - Working with datetime.time objects - timedelta

Just got a problem I don't understand in my python code.

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:

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

TypeError: combine() argument 2 must be datetime.time, not bytes

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

So I really don't understand it. It is datetime.time , but the same time it is not ... Can anyone help me?

Thanks!

Some of your objects are bytes objects, and others are datetime.time objects. 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. Ergo, start_timestamps[i] was a time object. If all objects in timestamps where bytes objects, datetime.combine(d, start_timestamps[i]) would have failed.

You need to figure out why your ts.time() calls in your map() call return inconsistent data types.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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