Python Forum
re.search and datetime not printing as expected
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
re.search and datetime not printing as expected
#1
how come its not printing as expected
text = "set a reminder for 10:02 p.m. "
match = re.search('\d{2}:\d{2}',text)
time_ = datetime.strptime(match.group(), '%I:%M').date()
print(time_)
this is what im getting when i print it
1900-01-01
Reply
#2
Because the default date when one isn't set is Jan 01, 1900. You didn't set a date, you only set a time. And then you asked it to print the date (not the time).

Change the end of line 3 to be .time() instead of .date() and you'll see that you've set the time (although you're not handling am/pm differences).
Reply
#3
yes, I have corrected the .time thank you for reply however regarding the am pm the only format is see is for am and pm with out the dots not like a.m. dot p.m.

also I have one more question say I want to search for time in a text in general there is a couple different ways the time would be displayed when using a speech recognizer for example if you say 10:00 am it will print 10 a.m. or if i say 1:07 pm it prints 1:07 p.m. the problem with searching for time with how i have it set up now is there are 4 different ways it could be searched.

example:
1 p.m.
1:07 p.m.
10 p.m.
10:07 p.m.

I'm referring to how the parameters of the re.search and how they are searching for time in text. I've tired using if statements for all of the different ways it could be written but this lead to two problems the code being over complicated and it throwing error codes do you have any idea?
Reply
#4
If you always have the a.m. or p.m. part, you could try a regex.

import re

times = ["1 p.m.",
         "1:07 p.m.",
         "10 p.m.",
         "10:07 p.m.",
         "Another format might be 12:37p.m.",
         "or perhaps it's only 8 a.m. today",
         "There's no time here 1 15 PM"]

pattern = r"(\d+)(?::(\d+))?\s*([ap]\.m\.)"
for text in times:
    m = re.search(pattern,text)
    if m:
        hour, minute, ampm = m.groups()
        print(f"It is {minute or 0} minutes after {hour} {ampm}")
    else:
        print("Couldn't find a time in the right format")
Output:
It is 0 minutes after 1 p.m. It is 07 minutes after 1 p.m. It is 0 minutes after 10 p.m. It is 07 minutes after 10 p.m. It is 37 minutes after 12 p.m. It is 0 minutes after 8 a.m. Couldn't find a time in the right format
Reply
#5
im sorry i think i explained it wrong
fmts1 = "%I"
fmts2 = "%I:%M"

text1 = "set a reminder for 1 p.m. "

match1 = re.search('\d{1}', text1)
time1 = datetime.strptime(match1.group(), fmts1).strftime(fmts1)
print(time1)

text2 = "set a reminder for 10 p.m. "

match2 = re.search('\d{2}', text2)
time2 = datetime.strptime(match2.group(), fmts1).strftime(fmts1)
print(time2)

text3 = "set a reminder for 1:02 p.m. "

match3 = re.search('\d{1}:\d{2}', text3)
time3 = datetime.strptime(match3.group(), fmts2).strftime(fmts2)
print(time3)

text4 = "set a reminder for 10:02 p.m. "

match4 = re.search('\d{2}:\d{2}', text4)
time4 = datetime.strptime(match4.group(), fmts2).strftime(fmts2)
print(time4)
so for each different way the text is displayed text 1 2 3 4 that's the way it could be written and I have each way it could be searched and what i want it to return but I want to combined all the search methods into one method that can search all the different ways it might be written. also the times that i have there are not defined it could be 2 p.m. it just that for each way its written the number holds a spot
Reply
#6
oh wait i think i see what you did my fault i understand it now thank you so much!!!
Reply
#7
one question though how you have patterns could you provide me with a good link or information artical on how that works so i can learn up on how to make search patterns in the future.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Search Results Web results Printing the number of days in a given month and year afefDXCTN 1 2,192 Aug-21-2020, 12:20 PM
Last Post: DeaD_EyE
  TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'str' findbikash 2 9,515 Sep-18-2019, 08:32 AM
Last Post: buran

Forum Jump:

User Panel Messages

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