Python Forum
getting error ValueError: time data '' does not match format '%H:%M'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
getting error ValueError: time data '' does not match format '%H:%M'
#1
I'm getting ValueError:time data ' ' does not match format '%H:%M'
for the following text file with text message
Quote:Cricket, a bat-and-ball park game played between two teams of eleven park
players on a field at the park center of which is a 20-metre (22-yard) pitch with
a wicket at each end, each park comprising two bails balanced on three stumps.
The batting park scores runs by striking the ball bowled at the park wicket with
the park bat, while the bowling and park fielding side tries to prevent this and
dismiss each park player (so they are "out"). Means of park include being
bowled, when the ball hits the park and dislodges the bails, and by the fielding
side park the ball after it is hit by the bat, but before it hits the park. When ten
park have been dismissed, the innings ends and the teams park roles.
from datetime import datetime


def SecretCode(file):
    lines = file.readlines()
    NumOfLines = str(len(lines))
    getTime = ''
    if (len(NumOfLines) > 2):
        getTime = NumOfLines[:2]+":"+NumOfLines[2:]
    elif(len(NumOfLines) == 2):
        getTime = NumOfLines[:2]+":"+"00"
    words = [word for line in lines for word in line.split()]
    Meeting_place = max(words, key=words.count)+" Street"
    d = datetime.strptime(getTime, "%H:%M")
    Meeting_time = d.strftime("%I:%M %p")
    print("Meeting time:{}".format(Meeting_time))
    print("Meeting place:{}".format(Meeting_place))


if __name__ == "__main__":
    fname = input()
    file = open(fname, "r")
    SecretCode(file)
Reply
#2
I think that error message is quite self-explanatory. Whatever your code does - you set 'getTime' to empty string then use if and elif (not else) to assign new value. If neither of these conditions are met 'getTime' value remain an empty string. And this is exactly what is happening, as you convert lenght of lines (9) to string which has length 1. This is not greater than two and not equal to two.
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
#3
from datetime import datetime


def SecretCode(file):
    lines = file.readlines()
    NumOfLines = str(len(lines))
    getTime = ''
    if (len(NumOfLines) > 2):
        getTime = NumOfLines[:2]+":"+NumOfLines[2:]
    elif(len(NumOfLines) == 2):
        getTime = NumOfLines[:2]+":"+"00"
    else:
        getTime = "0"+NumOfLines+":"+"00"
    words = [word for line in lines for word in line.split()]
    Meeting_place = max(words, key=words.count)+" Street"
    d = datetime.strptime(getTime, "%H:%M")
    Meeting_time = d.strftime("%I:%M %p")
    print("Meeting time:{}".format(Meeting_time))
    print("Meeting place:{}".format(Meeting_place))


if __name__ == "__main__":
    fname = input()
    file = open(fname, "r")
    SecretCode(file)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python date format changes to date & time 1418 4 518 Jan-20-2024, 04:45 AM
Last Post: 1418
  Export data from PDF as tabular format zinho 5 646 Nov-11-2023, 08:23 AM
Last Post: Pedroski55
  An unexplainable error in .format statement - but only in a larger piece of code? ToniE 4 656 Sep-05-2023, 12:50 PM
Last Post: ToniE
  Plotting by Time, error mansoorahs 1 707 May-16-2023, 09:46 AM
Last Post: Larz60+
  How to properly format rows and columns in excel data from parsed .txt blocks jh67 7 1,802 Dec-12-2022, 08:22 PM
Last Post: jh67
  Issue in changing data format (2 bytes) into a 16 bit data. GiggsB 11 2,560 Jul-25-2022, 03:19 PM
Last Post: deanhystad
  Invalid format specifier Error Led_Zeppelin 2 7,599 Jul-11-2022, 03:55 PM
Last Post: Led_Zeppelin
  What time format is this? wrybread 3 1,991 Jun-15-2022, 02:46 PM
Last Post: snippsat
  Date format error getting weekday value Aggie64 2 1,378 May-29-2022, 07:04 PM
Last Post: Aggie64
  Real time data satyanarayana 3 20,318 Feb-16-2022, 07:46 AM
Last Post: satyanarayana

Forum Jump:

User Panel Messages

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