Python Forum
Need to add hours min and seconds
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need to add hours min and seconds
#1
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!
Reply
#2
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)
Reply
#3
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)))
Reply
#4
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!
Reply
#5
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
Reply
#6
Thank you for the Code bowlofred, frank0903!
You guys awesome!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with module time and leap seconds Pedroski55 3 1,189 Oct-07-2022, 11:27 PM
Last Post: Pedroski55
  MatplotibTicks every 24 hours TamP 4 1,105 Oct-04-2022, 04:08 PM
Last Post: deanhystad
  Hello all! need help. calculating OT hours. no1up 5 1,428 Jul-30-2022, 10:00 PM
Last Post: rob101
  Store variable data and display sum after 60 seconds the_dude 11 3,367 Dec-16-2021, 07:07 PM
Last Post: deanhystad
  Py script that triggers ever 3 hours, but only between 9:15 am to 3:30 pm, Mon to Fri Pymod 2 1,791 Jun-30-2021, 05:14 PM
Last Post: Larz60+
  How to get utcnow + 2 hours? korenron 2 2,493 Mar-01-2021, 03:22 PM
Last Post: korenron
  How to calculate time difference between each row of dataframe in seconds Mekala 1 2,516 Jul-16-2020, 12:57 PM
Last Post: Larz60+
  Can't substract seconds from time Raj_Kumar 1 1,766 Apr-15-2020, 02:47 AM
Last Post: bowlofred
  How to calculate time in seconds rajeshE 1 2,078 Feb-15-2020, 11:07 PM
Last Post: Larz60+
  how to stop and start a script for 30 seconds laspaul 9 7,512 Jan-16-2020, 02:13 PM
Last Post: laspaul

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020