regex Python如何自动化字典的自定义Map

ftf50wuq  于 2023-03-09  发布在  Python
关注(0)|答案(1)|浏览(118)

我当前正在Map如下所示的数据

mapping = {'MarketResults_Feb22_AUCTION_Dec21Auc_Round_1.csv':datetime(2021,12,1),
            'MarketResults_Feb22_AUCTION_Nov21Auc_Round_1.csv':datetime(2021,11,1),
            'MarketResults_Jan22_AUCTION_Dec21Auc_Round_1.csv':datetime(2021,12,1),
            'MarketResults_Jan22_AUCTION_Nov21Auc_Round_1.csv':datetime(2021,11,1),
            'MarketResults_Spr22_AUCTION_Annual21Auc_Round_1.csv':datetime(2021,1,1),
            'MarketResults_Spr22_AUCTION_Annual21Auc_Round_2.csv':datetime(2021,1,2),
            'MarketResults_Spr22_AUCTION_Annual21Auc_Round_3.csv':datetime(2021,1,3),
            'MarketResults_Spr22_AUCTION_Oct21Auc_Round_1.csv':datetime(2021,10,1),
            'MarketResults_Win21_AUCTION_Annual21Auc_Round_1.csv':datetime(2021,1,1),
            'MarketResults_Win21_AUCTION_Annual21Auc_Round_2.csv':datetime(2021,1,2),
            'MarketResults_Win21_AUCTION_Annual21Auc_Round_3.csv':datetime(2021,1,3),
            'MarketResults_Win21_AUCTION_Oct21Auc_Round_1.csv':datetime(2021,10,1),
            'MarketResults_Spr22_AUCTION_Jan22Auc_Round_1.csv':datetime(2022,1,1),
            'MarketResults_Jan22_AUCTION_Jan22Auc_Round_1.csv':datetime(2022,1,1),
            'MarketResults_Feb22_AUCTION_Jan22Auc_Round_1.csv':datetime(2022,1,1)}

这已经变得非常麻烦,因为我的字典中可能有更多的文件,以至于不可行。Map背后的逻辑是对每月的文件使用“Auc”之前的日期。但是对于“年度”的文件,我会将它们保存为YYYYMMDD格式,年份是“年度”之后的数字,月份是“1”,日期是文件的整数。2下面是我尝试的代码,它适用于每月的文件,但我无法让它Map包含“年度”的文件。我使用的正则表达式旨在定位最接近'Auc'的日期。它匹配具有以下模式的字符串
x一个一个一个一个x一个一个二个x

ryevplcw

ryevplcw1#

三个捕获组如何,其中'ual'将被捕获为年度:

import re
import glob
from datetime import datetime
from pprint import pprint

folder_list = glob.glob('*MarketResults*')
mapping = {}

for filename in folder_list:
    match = re.search(r'(\w{3})(\d{2})Auc_Round_(\d+)', filename)
    text, digits, rnd = match.groups()
    if text == 'ual':
        dt = datetime(2000 + int(digits), 1, int(rnd))
    else:
        dt = datetime.strptime(text + digits, '%b%y').replace(day=int(rnd))
    mapping[filename] = dt

pprint(mapping)

输出:

{'MarketResults_Feb22_AUCTION_Dec21Auc_Round_1.csv': datetime.datetime(2021, 12, 1, 0, 0),
 'MarketResults_Feb22_AUCTION_Jan22Auc_Round_1.csv': datetime.datetime(2022, 1, 1, 0, 0),
 'MarketResults_Feb22_AUCTION_Nov21Auc_Round_1.csv': datetime.datetime(2021, 11, 1, 0, 0),
 'MarketResults_Jan22_AUCTION_Dec21Auc_Round_1.csv': datetime.datetime(2021, 12, 1, 0, 0),
 'MarketResults_Jan22_AUCTION_Jan22Auc_Round_1.csv': datetime.datetime(2022, 1, 1, 0, 0),
 'MarketResults_Jan22_AUCTION_Nov21Auc_Round_1.csv': datetime.datetime(2021, 11, 1, 0, 0),
 'MarketResults_Spr22_AUCTION_Annual21Auc_Round_1.csv': datetime.datetime(2021, 1, 1, 0, 0),
 'MarketResults_Spr22_AUCTION_Annual21Auc_Round_2.csv': datetime.datetime(2021, 1, 2, 0, 0),
 'MarketResults_Spr22_AUCTION_Annual21Auc_Round_3.csv': datetime.datetime(2021, 1, 3, 0, 0),
 'MarketResults_Spr22_AUCTION_Jan22Auc_Round_1.csv': datetime.datetime(2022, 1, 1, 0, 0),
 'MarketResults_Spr22_AUCTION_Oct21Auc_Round_1.csv': datetime.datetime(2021, 10, 1, 0, 0),
 'MarketResults_Win21_AUCTION_Annual21Auc_Round_1.csv': datetime.datetime(2021, 1, 1, 0, 0),
 'MarketResults_Win21_AUCTION_Annual21Auc_Round_2.csv': datetime.datetime(2021, 1, 2, 0, 0),
 'MarketResults_Win21_AUCTION_Annual21Auc_Round_3.csv': datetime.datetime(2021, 1, 3, 0, 0),
 'MarketResults_Win21_AUCTION_Oct21Auc_Round_1.csv': datetime.datetime(2021, 10, 1, 0, 0)}

相关问题