繁体   English   中英

对特定模式使用fnmatch

[英]Using fnmatch for this particular pattern

我正在尝试编写一个查找特定目录并查找特定文件格式匹配项的函数。

函数格式如下:

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

我当时正在考虑使用正则表达式,但是我不确定如何使用字符串数组。

假设您的函数需要返回数据的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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM