I want to add time durations attributes in an xml file using Python 2.7.
import xml.etree.ElementTree as ET
import time
for k in root.findall('TC'):
ttt= k.get('time')
s = time.strptime(ttt, "%H:%M:%S")
total_time = total_time + s
I can’t use + operator, the error is unsupported operand types (+) None_Type, time.struct_time.
How can I define total_time as a struct_time?
You’ll need to convert the
struct_timecomponents to adatetime.timedeltaobject to sanely deal with time durations:There is no easy way otherwise to convert the
struct_timeinformation to a duration; it is actually meant for date-time values, although using.strptime()to parse a duration is not too bad an idea.Your
total_timevalue is now adatetime.timedelta()object. To get the total number of seconds, use the.total_seconds()method on it: