Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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: ').

like image 460
ASH Avatar asked Dec 25 '18 05:12

ASH


4 Answers

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
    # ...
like image 147
alecxe Avatar answered Nov 12 '22 04:11

alecxe


Maybe to convert back to datetime type:

offset = str(link).find('Run:')
amount = offset + 15
pos = str(link)[offset:amount]
if datetime.strptime(pos,'%Y-%m-%d').isoweekday() in range(1, 6):
    outF.write(str(link))
    outF.write('\n')

Then it should work as expected.

like image 3
U12-Forward Avatar answered Nov 12 '22 05:11

U12-Forward


Let's suppose your link is

link = "Your Link String is Run:  2018-12-21 21:15:48"

Your following code will work well to find the offset starting from Run

offset = str(link).find('Run:')
amount = offset + 16

Since, there are two spaces after Run: hence, 16 needs to be added to offset. Now extracting exactly the date string 2018-12-21, we need to add 6 to offset as Run: has 6 character before starting the date string.

pos = str(link)[offset + 6:amount]

Now formatting our date string in an datetime object with

pos_date = datetime.strptime(pos, "%Y-%m-%d")

Remember to import datetime at the top of your program file as

from datetime import datetime

Now checking and displaying if the date is a weekday

if pos_date.isoweekday() in range(1, 6):
    print("It's a Week Day!")

This will return It's a Week Day!.

like image 2
Anidhya Bhatnagar Avatar answered Nov 12 '22 04:11

Anidhya Bhatnagar


link = "something something Run:  2018-12-24 ..."
offset = str(link).find('Run:') 
amount = offset +  15 # should be 16 
pos = str(link)[offset:amount] # this is a string

The pos of the example above will be Run: 2018-12-24, so it does not capture the date exactly.

A string object does not have isoweekday method, so pos.isoweekday() will result to error. But a datetime.datetime object does have that method.

A solution:

import datetime

link = "something something Run:  2018-12-24 ..."
offset = str(link).find('Run:') # will only give the index of 'R', so offset will be 20
amount = offset + 16
pos = str(link)[offset:amount] # pos is 'Run:  2018-12-24'
datestring = pos.split()[1] # split and capture only the date string

#now convert the string into datetime object
datelist = datestring.split('-')
date = datetime.datetime(int(datelist[0]), int(datelist[1]), int(datelist[2]))

if date.isoweekday() in range(1, 6):
    ....

This okay..?

like image 1
Arief Avatar answered Nov 12 '22 03:11

Arief