regex 如何验证以字符串表示的日期是否属于另一个字符串中表示的2个日期的间隔?

8cdiaqws  于 2023-02-10  发布在  其他
关注(0)|答案(2)|浏览(88)
import os, datetime

content = os.listdir("when")
print(content)
#this print...
#['2022_-_12_-_29 12pp33 am _--_ 2023_-_01_-_25 19pp13 pm.txt', '2023-02-05 00pp00 am.txt']

for i in range(len(content)):
    content[i] = content[i].replace("_-_", "-").replace("pp", ":")
print(content) #I prepare the input to use it to search
#this print...
#['2022-12-29 12:33 am _--_ 2023-01-25 19:13 pm.txt', '2023-02-05 00:00 am.txt']

input_to_search_in_folder = "2022_-_01_-_05 12:33 am"  #file data to find in the 'when' folder

我已经将:更改为pp(指点对点),因为不能将:放在文件夹或/和文件中,至少在Windows中不能

2022_-_12_-_29 12pp33 am _--_ 2023_-_01_-_25 19pp13 pm
            initial date _--_ final date

在这种情况下,input_to_search_in_folder = "2022_-_01_-_05 12:33 am"与具有特定日期名称的文件不匹配,但如果它属于文件名'2022_-_12_-_29 12pp33 am _--_ 2023_-_01_-_25 19pp13 pm.txt'中指定的天数间隔
我如何验证这个日期"2022_-_01_-_05 12:33 am"是否属于那个时间间隔'2022_-_12_-_29 12pp33 am _--_ 2023_-_01_-_25 19pp13 pm',或者它是否是这个日期'2023-02-05 00:00 am'
如果验证成功,程序应该打印该.txt文件中的内容(在本例中是2022_-_12_-_29 12pp33 am _--_ 2023_-_01_-_25 19pp13 pm.txt文件)

text_file = open("when/" + , "r")
data_inside_this_file = text_file.read()
text_file.close()

#And finally prints the content of the .txt file that matches the date specified in the 'input_to_search_in_folder' variable
print(repr(data_inside_this_file))
ajsxfq5m

ajsxfq5m1#

我会彻底清理字符串,将它们转换为datetime对象(因为它们可以相互比较),然后比较,您就得到了结果,可以对它做任何事情:

import os
from datetime import datetime

content = os.listdir("when")
print(content)
#['2022_-_12_-_29 12pp33 am _--_ 2023_-_01_-_25 19pp13 pm.txt', '2023-02-05 00pp00 am.txt']

for i in range(len(content)):
    content[i] = content[i].replace("_-_", "-").replace("pp", ":")
#['2022-12-29 12:33 am _--_ 2023-01-25 19:13 pm.txt', '2023-02-05 00:00 am.txt']

cleaned_filename = os.path.splitext(content[0])[0] #="2022-12-29 12:33 am _--_ 2023-01-25 19:13 pm"
start_dt = datetime.strptime(content[0].split(" _--_ ")[0], "%Y-%m-%d %H:%M")
#="2022-12-29 12:33 am" = datetime(2022, 12, 29, 12, 33)
last_dt = datetime.strptime(content[0].split(" _--_ ")[1], "%Y-%m-%d %H:%M")
#="2023-01-25 19:13 pm"
third_dt = datetime.strptime(os.path.splitext(content[1])[0], "%Y-%m-%d %H:%M")
#="2023-02-05 00:00 am"

input_to_search = "2022_-_01_-_05 12:33 am".replace("_-_", "-") 
#"2022-01-05 12:33 am".
input_dt = datetime.strptime(input_to_search, "%Y-%m-%d %H:%M")
#="datetime(2022, 01, 05, 12, 33)"

if start_dt <= input_dt <= last_dt:
    print("in between")
elif input_dt == third_dt:
    print("Match")
else:
    print("No!")
ryoqjall

ryoqjall2#

一种方法是使用regex提取日期,然后像mrblue6的答案那样将它们转换为date:

#!/usr/bin/python3

from datetime import datetime
import re

# Let's assume this is one of the directory entries
direntry='2022_-_12_-_29 12pp33 am _--_ 2023_-_01_-_25 19pp13 pm.txt'

# We exclude in the regex the AM/PM part since the format is 24-hour clock
datePattern = '(\d{4}_-_\d{2}_-_\d{2} \d{2}pp\d{2}) [ap]m'
dirPattern =  f'{datePattern} _--_ {datePattern}.txt'

# Let's extract the "milestone" dates
matches = re.search(dirPattern, direntry)
extractedDate1 = matches.group(1)
extractedDate2 = matches.group(2)

# Let's extract the date to check
matches = re.search(datePattern, "2022_-_01_-_05 12pp33 am")
extractedDateToCheck = matches.group(1)

# Let's convert them as date time
readDateFormat = '%Y_-_%m_-_%d %Hpp%M'
date1 = datetime.strptime(extractedDate1, readDateFormat)
date2 = datetime.strptime(extractedDate2, readDateFormat)
dateToCheck = datetime.strptime(extractedDateToCheck, readDateFormat)

# Let's compare them
print (f"Date 1 :        {date1}")
print (f"Date 2 :        {date2}")
print (f"Date to check:  {dateToCheck}")
print (f"Check:          {date1 <= dateToCheck <= date2}" )

输出:

Date 1 :        2022-12-29 12:33:00
Date 2 :        2023-01-25 19:13:00
Date to check:  2022-01-05 12:33:00
Check:          False

相关问题