0

I'm having a lot of trouble figuring out how to figure out this file pattern. I have the following code:

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

This is a funciton that's part of a class. There's one issue. I just realized that the parameter fundCode will actually be an array of values, so I need to use some sort of delimiter. In the end, I want to look for files matching this kind of pattern:

unmapped_FUND1_FUND2_FUNDETC_20180203_20180204.CSV or

unmapped_FUND1_20180203_20180204.CSV

I'm guessing regex would be a good use for this?

2 Answers 2

1

You could try join.

fundCode_raw = ['FUND1','FUND2','FUNDETC']
fundCode_str = '_'.join(fundCode_raw)

>> fundCode_str
'FUND1_FUND2_FUNDETC'
Sign up to request clarification or add additional context in comments.

Comments

0

You shouldn't need regex just to see if a file with that name exists. You can just construct the name of the file you're looking for (with the appropriate path), and then test to see if it exists.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.