Python Forum
Warning - ' invalid escape sequence '\s''
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Warning - ' invalid escape sequence '\s''
#1
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
Reply
#2
Use raw strings for your pattern so \ is not interpreted as the start of an escape sequence
tester_V likes this post
Reply
#3
Will do that. I tried to run same thing using Python 310, and it runs without any warnings or errors.
That strange. Right?
Reply
#4
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.
tester_V likes this post
Reply
#5
Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  use of escape character in re.sub and find WJSwan 1 1,575 Feb-16-2023, 05:19 PM
Last Post: Larz60+
  WARNING: Ignoring invalid distribution kucingkembar 1 30,665 Sep-02-2022, 06:49 AM
Last Post: snippsat
  Escape indentation Frankduc 11 4,928 Jan-31-2022, 02:41 PM
Last Post: Frankduc
  add Escape charcters in string GrahamL 3 1,911 Jan-20-2022, 01:15 PM
Last Post: GrahamL
  Escape Single quotation between each content tag usman 3 3,658 May-02-2021, 03:32 PM
Last Post: snippsat
  DIY Escape Room for fun StannemanPython 1 3,979 Feb-17-2021, 10:53 PM
Last Post: maurom82
  How to escape OrderedDict as an argument? Mark17 2 2,679 Dec-23-2020, 06:47 PM
Last Post: Mark17
  zlib decompress error: invalid code lengths set / invalid block type DreamingInsanity 0 9,105 Mar-29-2020, 12:44 PM
Last Post: DreamingInsanity
  help for escape sequences NewPi 1 2,649 Dec-11-2019, 11:22 PM
Last Post: ichabod801
  escape single quote deep_logic 1 2,313 Sep-10-2019, 08:05 PM
Last Post: SheeppOSU

Forum Jump:

User Panel Messages

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