Python Forum

Full Version: Need to add hours min and seconds
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I have a problem adding HHMMSS.
Have a file, it goes like this:

5/1/2020 2:40:31 PM, 5/1/2020 2:43:11 PM, 00:02:40.5449136, PV0015EN,Cell_13, TT
5/1/2020 2:45:26 PM, 5/1/2020 2:48:17 PM, 00:02:50.8250927, PV0015EN,Cell_13, TT
5/1/2020 2:53:18 PM, 5/1/2020 2:56:01 PM, 00:02:43.7207947, PV0015EN,Cell_13, TT
5/1/2020 3:04:43 PM, 5/1/2020 3:05:42 PM, 00:00:59.2069877, PV0015EN,Cell_13, TT
5/1/2020 2:53:18 PM, 5/1/2020 2:56:01 PM, 00:02:43.7207947, PV0015EN,Cell_13, TT
5/1/2020 3:04:43 PM, 5/1/2020 3:05:42 PM, 00:00:59.2069877, PV0015EN,Cell_13, TT

The third column is a time duration and I need to sum it up.
I thought I will split each line, get the third column (timeduration) then will split it (will remove milliseconds) and add separately Minutes and Seconds.
And after that will convert using something like this:
sec= 66
fg= str(datetime.timedelta(seconds=sec))
print (fg)

min= 25
fm=str(datetime.timedelta(minutes=min))
print (fm)
I created little script but stuck Wall - do not know how to add my vars:
Her is my code:

import os
too = 'C:/01/OUTCOME.txt'

with open(too) as fo:        
            for line in fo:
                tu, on,ontu,*stu= line.split(',')
                ontu=ontu.rstrip()
                sd,st=ontu.split('.')
                #print (sd)
                sdh,sdm,sds=sd.split(':')
                print ("Minutes", sdm)
                print ("Seconds", sds)
Thanks in advance!
Pretty close. Create a "total" to hold the summation:

totaltime = datetime.timedelta(0)
Then just add the timedelta to it from your split out items. Note you've split out a string, so have to convert it to a number.

                [...]
                totaltime += datetime.timedelta(minutes=int(sdm), seconds=int(sds))

print(totaltime)
Also, if you want, there's no need to ignore the milliseconds. You could just keep the decimal, then convert to a float instead of an int.

total = datetime.timedelta(0)
with open(too) as fo:
            for line in fo:
                tu, on,ontu,*stu= line.split(',')
                ontu=ontu.rstrip()
                sdh,sdm,sds=ontu.split(':')
                print ("Minutes", sdm)
                print ("Seconds", sds)
                total += datetime.timedelta(minutes=float(sdm), seconds=float(sds))

print(total)
import os
too = r'C:/01/OUTCOME.txt'
with open(too) as fo:        
    sec = 0
    for line in fo:
        res = line.split(',')
        ontu=res[2].strip()
        sd,st=ontu.split('.')
        sdh,sdm,sds=sd.split(':')
        sec += int(sdh)*3600 + int(sdm)*60 + int(sds)
    print(str(datetime.timedelta(seconds=sec)))
For whatever reason, both snippets producing errors, see below.

snippet 1 error:
total = datetime.timedelta(0)
NameError: name 'datetime' is not defined


snippet 2 error:
print(str(datetime.timedelta(seconds=sec)))
NameError: name 'datetime' is not defined

Did I miss a module?

Thank you for the snippets!

My bad!
I added "import datetime" and now all is working!
Thank you very much for your help!
You need datetime. Since you mentioned datetime.timedelta() in your first post, I assumed you were already using it in some form. But yes, it must be imported.

import datetime
Thank you for the Code bowlofred, frank0903!
You guys awesome!