How to search for a substring, find the beginning and ending, and then check if that data is a weekday?

  • A+

I've come up with the following which should be fairly close, but it's not quite right. I am getting the following error when I try to test if the data is a weekday. AttributeError: 'str' object has no attribute 'isoweekday'

Here is my feeble code:

offset = str(link).find('Run:') amount = offset + 15 pos = str(link)[offset:amount] if pos.isoweekday() in range(1, 6):     outF.write(str(link))     outF.write('/n') 

I'm looking for the string 'Run: ' (it always has 2 blanks after the colon) and then I want to move 15 spaces to the right, to capture the date. So, n-number of spaces to find 'Run: ' and then get the date, like '2018-12-23' and test if this date is a weekday. If this substring is a weekday, I want to write the entire string to a line in a CSV file (the writing to a CSV file works fine). I'm just not sure how to find that one date (there are several dates in the string; I need the one immediately following 'Run: ').


You've only forgotten to load it into a datetime object:

from datetime import datetime  # ... pos_date = datetime.strptime(pos, "%Y-%m-%d") if pos_date.isoweekday() in range(1, 6):       # ... 

Also, as you are using .isoweekday() and Monday is represented as 1, you don't really need to check the lower boundary:

if pos_date.isoweekday() <= 5:  # Monday..Friday     # ... 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: