Python Forum

Full Version: Warning - ' invalid escape sequence '\s''
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Greetings!
I'm looking for the tree specific lines in my files (see below), and I have a regex that gets me the lines but it also prints warnings in the beginning of the script '' invalid escape sequence '\s''.
I'm using Python 3.12.1
It seems a simple problem but I'm struggling with this for some reason.
Could you guys help me wiht this?
The lines actually have a lot of spaces before the Start/End/Run words for some reason when I paste them they are disappear,
I inserted hyphens instead of the white spaces.
-------------------------------- Start Time 3/6/2023 11:18:35 PM
--------------------------------- End Time 3/6/2023 12:25:24 AM
--------------------------------- Run Time 01:06:49.0905697
RegEx:
if re.findall("Start\s+Time\s+\d",el)
elif re.findall("End\s+Time\s+\d",el) 
elif re.findall("Run\s+Time\s+\d",el)
Thank you.
Tester_V
Use raw strings for your pattern so \ is not interpreted as the start of an escape sequence
Will do that. I tried to run same thing using Python 310, and it runs without any warnings or errors.
That strange. Right?
Maybe this might help:

import re

s = """                                  Start Time 3/6/2023 11:18:35 PM
                                  End Time 3/6/2023 12:25:24 AM
                                  Run Time 01:06:49.0905697"""


e = re.compile(r'(Start\s+Time)\s+(\d+/\d+/\d+)\s+(\d+:\d+:\d+)\s+(AM|PM)')
res = e.search(s)
res.groups() # returns a tuple ('Start Time', '3/6/2023', '11:18:35', 'PM')
res = e.findall(s) # returns a list [('Start Time', '3/6/2023', '11:18:35', 'PM')]
I think I would go through the text line by line and save what you want from res.groups().

Easy to make similar expressions for End Time and Run Time!

By converting to datetime objects you can calculate the runtime, but maybe you are just interested in the dates.
Thank you!