使用正则表达式匹配变量的文件模式

时间:2018-07-06 14:59:22

标签: python regex

我在弄清楚如何弄清楚这个文件模式时遇到了很多麻烦。我有以下代码:

def file_pattern_match(self, fundCode, startDate, endDate):

    # check if the fundcode is in the array or if the fundcode matches one in the array
    fundCode = fundCode if fundCode in self.fundCodes else 'Invalid_Fund_Code'

    # set a file pattern
    file_pattern = 'unmapped_{fund}_{start}_{end}.csv'.format(fund=fundCode, start=startDate, end=endDate) 

    # look in the unmappedDir and see if there's a file with that name

    # if the there is load the positions
    pass

这是类的一部分。有一个问题。我刚刚意识到参数fundCode实际上将是一个值数组,因此我需要使用某种分隔符。最后,我要查找与这种模式匹配的文件:

unmapped_FUND1_FUND2_FUNDETC_20180203_20180204.CSV

unmapped_FUND1_20180203_20180204.CSV

我猜正则表达式会很好用吗?

2 个答案:

答案 0 :(得分:1)

您可以尝试<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <div id="test"></div> <select id="select1" disabled> <option value='1'>apple</option> <option value='2'>banana</option> </select> </form>

join

答案 1 :(得分:0)

您不需要仅使用正则表达式即可查看是否存在具有该名称的文件。您可以只构建要查找的文件的名称(使用适当的路径),然后进行测试以查看其是否存在。

import os
path = "unmappedDir/unmapped_{fund}_{start}_{end}.csv".format(fund = "_".join(fundCode), start = startDate, end = endDate)
if os.path.isfile(path):
    # Do loading.