Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Date format issue
#1
Hi All,

My apologies in advance if my code is sloppy, just getting back into programming from a LONG break.

I'm having some trouble with datetime.strptime module in my Python3 script - which is running on a Raspberry Pi 3. I keep getting the error 'ValueError: time data '' does not match format '%Y-%m-%d %H:%M:%S.%f''

The trouble code in my script is as follows-

from datetime import datetime, timedelta

if os.path.isfile(log_file): # First check if file exists.
        with open(log_file, 'r') as f:
            date = f.read()
            date_to_datetime = datetime.strptime(date, "%Y-%m-%d %H:%M:%S.%f")

            if datetime.now() < date_to_datetime + timedelta(minutes=1):
                return

    # Update the email log.
    with open(log_file, 'w') as f:
        f.write(str(datetime.now()))
Any and all help would be greatly appreciate. If you have a suggestion on how to fix my issue, please revise my code so it's as straight forward for me to understand as best as possible. Thank you!

- TD
Reply
#2
Replace
date_to_datetime = datetime.strptime(date, "%Y-%m-%d %H:%M:%S.%f")
with
try:
    date_to_datetime = datetime.strptime(date, "%Y-%m-%d %H:%M:%S.%f")
except ValueError as exc:
    raise ValueError('Bad datetime:', date) from exc
Reply
#3
Show us how your data looks in the file. Error is clear, format string don't match your data
Also, you should get SynraxError return outside functiom before anything else
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
My wild guess would be that f.read() would return string with newline at end. This may cause trouble for strptime().

Documentations states:

Output:
ValueError is raised if the date_string and format can’t be parsed by time.strptime() or if it returns a value which isn’t a time tuple.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
@Gribouillis I tried your suggested method but received a syntax error message. Any thoughts on that?

@perfringo yes, that's probably the issue which is why I'm seeking help. Any helpful suggestion?

@buran my opencv dual USB camera motion detection module code is below-

Module 1 (main USB camera executable):

# import the necessary packages
from __future__ import print_function
from pyimagesearch.basicmotiondetector import BasicMotionDetector
from imutils.video import VideoStream
from send_mail import send_mail
import numpy as np
import datetime
import imutils
import time
import cv2
 
# initialize the video streams and allow them to warmup
print("[INFO] starting cameras...")
webcam1 = VideoStream(src=0).start()
webcam2 = VideoStream(src=4).start()#src 0,2,4 work
time.sleep(2.0)
 
# initialize the two motion detectors, along with the total
# number of frames read
camMotion1 = BasicMotionDetector()
camMotion2 = BasicMotionDetector()
total = 0
# loop over frames from the video streams
while True:
    # initialize the list of frames that have been processed
    frames = []
 
    # loop over the frames and their respective motion detectors
    for (stream, motion) in zip((webcam1, webcam2), (camMotion1, camMotion2)):
        # read the next frame from the video stream and resize
        # it to have a maximum width of 400 pixels
        frame = stream.read()
        frame = imutils.resize(frame, width=400)
 
        # convert the frame to grayscale, blur it slightly, update
        # the motion detector
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        gray = cv2.GaussianBlur(gray, (21, 21), 0)
        locs = motion.update(gray)
 
        # we should allow the motion detector to "run" for a bit
        # and accumulate a set of frames to form a nice average
        if total < 32:
            frames.append(frame)
            continue
        # otherwise, check to see if motion was detected
        if len(locs) > 0:
            # initialize the minimum and maximum (x, y)-coordinates,
            # respectively
            (minX, minY) = (np.inf, np.inf)
            (maxX, maxY) = (-np.inf, -np.inf)
 
            # loop over the locations of motion and accumulate the
            # minimum and maximum locations of the bounding boxes
            for l in locs:
                (x, y, w, h) = cv2.boundingRect(l)
                (minX, maxX) = (min(minX, x), max(maxX, x + w))
                (minY, maxY) = (min(minY, y), max(maxY, y + h))
 
            # draw the bounding box
            cv2.rectangle(frame, (minX, minY), (maxX, maxY),
                (0, 0, 255), 3)
            
            send_mail(frame=frame)
        
        # update the frames list
        frames.append(frame)
        # increment the total number of frames read and grab the 
    # current timestamp
    total += 1
    timestamp = datetime.datetime.now()
    ts = timestamp.strftime("%Y-%m-%d %H:%M:%S.%f")
 
    # loop over the frames a second time
    for (frame, name) in zip(frames, ("Webcam1", "Webacm2")):
        # draw the timestamp on the frame and display it
        cv2.putText(frame, ts, (10, frame.shape[0] - 10),
            cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 255), 1)
        cv2.imshow(name, frame)
 
    # check to see if a key was pressed
    key = cv2.waitKey(1) & 0xFF
 
    # if the `q` key was pressed, break from the loop
    if key == ord("q"):
        break
 
# do a bit of cleanup
print("[INFO] cleaning up...")
cv2.destroyAllWindows()
webcam1.stop()
webcam2.stop()
Module 2 (email module after motion is detected)-

# STANDARD LIBRARY IMPORTS
import smtplib
from datetime import datetime, timedelta
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import os
import sys

# THIRD PARTY IMPORTS
import cv2

# gmail must be set to allow less secure apps, so you can send emails.
# set it up on:
# https://myaccount.google.com/u/1/lesssecureapps?pli=1


def send_mail(frame):

    path = os.path.dirname(sys.argv[0])
    log_file = path + '/email.log'

    # Check if the last email was sent in an 1 minute tolerance.
    # We are going to use a .log file to keep track of it.
    if os.path.isfile(log_file): # First check if file exists.
        with open(log_file, 'r') as f:
            date = f.read()
            date_to_datetime = datetime.strptime(date, "%Y-%m-%d %H:%M:%S.%f")

            if datetime.now() < date_to_datetime + timedelta(minutes=1):
                return

    # Update the email log.
    with open(log_file, 'w') as f:
        f.write(str(datetime.now()))

    # Create the jpg picture to attach.
    cv2.imwrite("project/intrude.jpg", frame)

    gmail_user = 'my_email'
    gmail_password = 'password'
    recipient = 'to_email'
    message = 'Hey! It appears that someone is at home!!!'

    msg = MIMEMultipart()
    msg['From'] = gmail_user
    msg['To'] = recipient
    msg['Subject'] = "Someone is at Home!"
    msg.attach(MIMEText(message))

    # Attachment
    file = path + '/intrude.jpg'
    filename = 'intrude.jpg'
    attachment = open(file, "rb")

    part = MIMEBase('application', 'octet-stream')
    part.set_payload(attachment.read())
    encoders.encode_base64(part)
    part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
    msg.attach(part)

    mail_server = smtplib.SMTP('smtp.gmail.com', 587)
    mail_server.ehlo()
    mail_server.starttls()
    mail_server.ehlo()
    mail_server.login(gmail_user, gmail_password)
    mail_server.sendmail(gmail_user, recipient, msg.as_string())
    mail_server.close()
Reply
#6
(Jun-05-2019, 01:01 PM)the_dude Wrote: @buran my opencv dual USB camera motion detection module code is below-
date (as read from the file) will have newline char at the end. you need to strip that before converting to datetime object
we should have seen that from your original post...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
the_dude Wrote:I tried your suggested method but received a syntax error message. Any thoughts on that?
I suspect you are using an outdated version of python. You can try to remove the as exc and from exc part of the suggestion.
Reply
#8
@buran that's exactly what I thought was happening. Do you have any example codes you could share with me that would fix my issue?
Reply
#9
Your error ValueError: time data '' show that your read of log_file return an empty string.
date = f.read() # return ''
Make the same error.
>>> date = ''
>>> datetime.strptime(date, "%Y-%m-%d")
Error:
Traceback (most recent call last): File "<interactive input>", line 1, in <module> File "C:\python37\lib\_strptime.py", line 577, in _strptime_datetime tt, fraction, gmtoff_fraction = _strptime(data_string, format) File "C:\python37\lib\_strptime.py", line 359, in _strptime (data_string, format)) ValueError: time data '' does not match format '%Y-%m-%d'
With a date it work.
>>> date = '2019-03-11'
>>> datetime.strptime(date, "%Y-%m-%d")
datetime.datetime(2019, 3, 11, 0, 0)
Reply
#10
Oh, I didn't pay enough attention .
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 228 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Python date format changes to date & time 1418 4 605 Jan-20-2024, 04:45 AM
Last Post: 1418
  Modifying a date format jehoshua 17 2,978 Oct-29-2022, 08:44 PM
Last Post: jehoshua
  Issue in changing data format (2 bytes) into a 16 bit data. GiggsB 11 2,662 Jul-25-2022, 03:19 PM
Last Post: deanhystad
  Date format error getting weekday value Aggie64 2 1,425 May-29-2022, 07:04 PM
Last Post: Aggie64
  Convert Date to another format lonesoac0 2 1,672 Mar-17-2022, 11:26 AM
Last Post: DeaD_EyE
  Format SAS DATE Racer_x 0 996 Feb-09-2022, 04:44 PM
Last Post: Racer_x
  Date Conversion issue Racer_x 2 1,786 Jan-10-2022, 02:40 PM
Last Post: Racer_x
  How can I compare 2 format of date? korenron 4 1,525 Dec-21-2021, 12:40 PM
Last Post: korenron
  Date format and past date check function Turtle 5 4,256 Oct-22-2021, 09:45 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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