假设文本中的所有日期都在年、月、周的基数日
格式(您必须替换在里面具有属于第二天):
导入日历进口再进口文本=[“2021年9月的第一个星期三”,“2022年7月第三个星期一”,#更多日期]pattern=r“(w+),(\d{4})的(\w+)(\w+])”基数={“第一”:1,“秒”:2,“第三”:3,“第四”:4,“第五”:5}定义查找周的第n天(年、月、日、周):年=int(year_str)month=列表(calendar.month_name).index(month_name.capitalize())如果月份==0:return无n=基数.get(n_str.lower())如果n为无:return无cal=日历月日历(年、月)day_index=列表(calendar.day_name).index(day_of_week.capitalie())nth_occurrence=[week[day_index]代表一周,如果是week[day_ndex]!=0]如果n>len(第n次出现):return无天=第n次出现[n-1]date=f“{calendar.month_abbr[month]}{day},{year}”返回日期定义parse_text(文本):match=重新匹配(模式、文本)如果匹配:基数,day_of_week,month,year=match.groups()return find_nth_day_of_week(年、月、周、基数)return无日期=[parse_text(block)for block in text]对于i,枚举中的日期(日期):打印(f“日期{i+1}:{Date}”)