我正在尝试编写一个可以查询特定目录并查找特定文件格式匹配项的函数。
函数格式如下:
def file_pattern_match(self, fundCodes, startDate, endDate):
# set a file pattern
file_pattern = 'unmapped_{fund}_{start}_{end}.csv'.format(fund=somethinghere, 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
这是我有点困惑的地方。 fundCodes
将是一个FundCodes数组。因此,此功能将必须搜索目录中的文件,并查看是否存在匹配项。
称呼它:
file_pattern_match(['FUND1', 'FUND2', 'FUND3'], '20180203', '20180204')
应该找到这样的文件:
unmapped_FUND1_20180203_20180204.csv
我当时正在考虑使用正则表达式,但不确定如何处理字符串数组。
答案 0 :(得分:0)
假设您的函数需要返回数据的DataFrame,并且只有一个代码与函数看起来像的文件匹配
import pandas as pd
import os
def file_pattern_match(self, fundCodes, startDate, endDate):
# Get a list of the files
files = os.listdir(unmappeddir)
# loop through the codes and chack if they are in the files
for check_fund in fundCodes:
# set a file pattern
file_pattern = 'unmapped_{fund}_{start}_{end}.csv'.format(fund=check_fund,
start=startDate, end=endDate)
# look in the unmappeddir and see if there's a file with that name
if file_pattern in files:
return pd.read_csv(file_pattern)