Oops,

"items = dstr[1:-2].split(',')"

should have read

"items = dstr[1:-1].split(',')".

On 12/15/2022 1:56 PM, Thomas Passin wrote:
It's hard to be sure from what you have offered, but I suspect that you are taking the string "(2022, 12, 13,  5,  3, 30)" from the file and using it as is.  When you feed that in as a starred argument, the string gets treated as a sequence where each item is a character in the string.  Your example contains 26 characters, which matches the error message, so that's probably what is going on.

You need to convert the string into the correct integers, because is the datetime function expects to get integers, not strings.  It isn't going to work with a string that looks like a tuple when it is printed.

Here is one way you could do this.  From the input file, extract the string. Call it dstr.  Then you have to get rid of the parentheses and separate out each item so you can convert it into an integer.  So:

items = dstr[1:-2].split(',')  # This creates a list of strings.
# print(items) --> ['2022', ' 12', ' 13', '  5', '  3', ' 3']

# Create a tuple of integers from the string items
seq = (int(n) for n in items)
# or make it a list instead: seq = [int(n) for n in items]

# And here is the datetime object you wanted
d1 = datetime.datetime(*seq)


On 12/15/2022 1:14 PM, Gronicus@SGA.Ninja wrote:
So far so good , I can now use a variable in datetime.datetime but it only works if I hard-code the time/date information. Now I want to have the code
read from a file but I get: TypeError: function takes at most 9 arguments
(26 given)

I figure that the structure in the file is incorrect. What should it be? The entry in the file is (2022, 12, 13,  5,  3, 30) but when my program tries to
use it I get the error.

The program is a bit more sophisticated now but here is the update with a
sample of the SPECIFICATIONS.txt file:
=====================================================================

# This program compares two Timedate values, subtracts the two and
# converts the difference to seconds and hours.
#

#  %A Monday    #  %a Mon           #  %B January   #  %b Jan
#  %d 05 day    #  %m month as 01   #  %Y 2020      #  %y 20
#  %H 24        #  %I 12            #  %M 30 min    #  %S Seconds

import time
import datetime
from time import gmtime, strftime ##define strftime as time/date right now
# ======================================================

def GetSpecByItem(GetThisOne):  #get line by item in column 4 - 7
     ItemValue = "--"
     with open("SPECIFICATIONS.txt" , 'r') as infile:
      for lineEQN in infile: # loop to find each line in the file for that
dose
         if ((lineEQN[4:7]== GetThisOne)):
            ItemValue = lineEQN[30:60].strip()   # Just the Data
     return(ItemValue)

"""
SPECIFICATIONS.txt

IYf HRB Humalog R Date           (2018, 12, 4, 10,  7, 00)           ##
IYf HRG Humulin R Date           (2022, 12, 13,  5,  3, 30)          ##
"""
# ====================== Main() ======================================
print()
Startt = "404"
Stopp  = "404"

Answer = "Y"
Answer = input("Run test A? (" + Answer + ")" )

if Answer == "Y" or Answer == "y" or Answer == "":
    print()
    print("     Running Test A:")
#           Year  Mth Day Hour Min Sec
    Startt  =   2018, 12, 4, 10,  7, 00
    Stopp   =   2022, 12, 12, 1, 15, 30
    NowTime =   2022, 12, 14, 21, 15, 30
else:
    print("     Running Test B:")
    Startt = GetSpecByItem("HRG")
    Stopp =  GetSpecByItem("HRB")
    NowTime = strftime("(%Y, %m, %d, %H, %M, %S)")
print()
print("55    NowTime = " + str(NowTime))
print("56     Startt = " + str(Startt))
print("57      Stopp = " + str(Stopp))
print()

NowTime =  datetime.datetime(*NowTime)
Startt =   datetime.datetime(*Startt)
Stopp =    datetime.datetime(*Stopp)

#Start == Startt  # True"
#print("Startt test = " + Start)
# =================================================
print()
c = NowTime - Stopp
minutes = c.total_seconds() / 60
minutes = c.seconds / 60
hours = 0

while (minutes > 59):
         minutes = minutes - 60
         hours += 1
minutes = round(minutes)
print ("77 Hours =     <" + str(hours) + ">")
print ("78 Minutes =   <" + str(minutes) + ">")
if hours > 7:
     print(" Time to inject Humulin R u500.")

pause = input("Pause")
# ======================================================


-----Original Message-----
From: Python-list <python-list-bounces+gronicus=sga.ni...@python.org> On
Behalf Of Thomas Passin
Sent: Tuesday, December 13, 2022 11:20 PM
To: python-list@python.org
Subject: Re: Subtracting dates to get hours and minutes

Your problem is that datetime.datetime does not accept a tuple as an
argument.  It expects an integer value for the first argument, but you
supplied a tuple.  In Python, you can use a sequence (e.g., tuple or
list) the way you want by prefixing it with an asterisk.  This causes the
sequence of items to be treated as individual arguments. So:

Startt = datetime.datetime(2022, 12, 13,  5,  3, 30)
st1 = (2022, 12, 13,  5,  3, 30)
dts1 = datetime.datetime(*st1)  # NOT datetime.datetime(st1)
dts1 == Startt  # True

On 12/13/2022 10:43 PM, Gronicus@SGA.Ninja wrote:
   As is, Test A works.
   Comment out Test A and uncomment Test B it fails.
   In Test B, I move the data into a variable resulting with the report:
              "TypeError: an integer is required (got type tuple)

How do I fix this?

#---------------------------------------------------------------------
--------
import datetime
#=================================================
#         Test A   Hard coded Date/Time
Startt = datetime.datetime(2022, 12, 13,  5,  3, 30) Stopp =
datetime.datetime(2022, 12, 12, 21, 15, 30)

# =================================================
#         Test B   Date/Time data as a variable
#Startt = (2022, 12, 13,  5,  3, 30)
#Stopp =  (2022, 12, 12, 21, 15, 30)

#Startt = datetime.datetime(Startt)
#Stopp =  datetime.datetime(Stopp)

# =================================================
c = Startt - Stopp
minutes = c.total_seconds() / 60
minutes = c.seconds / 60
hours = 0

while (minutes > 59):
          minutes = minutes - 60
          hours += 1
minutes = round(minutes)
print()
print ("       Hours =  <" + str(hours) + ">")
print ("     Minutes =  <" + str(minutes) + ">")

#
----------------------------------------------------------------------
-------


--
https://mail.python.org/mailman/listinfo/python-list



--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to