兼容读取 csv &excel 文件

def read_file(self,**kwargs):
    '''
    兼容读取csv excl格式文件
    '''
    src_path = kwargs.pop('src_path',None)
    exclude_excel = kwargs.pop('exclude_excel',list)
    ceclude_csv = kwargs.pop('exclude_csv',list)
    if src_path.lower().endswith('.xlsx'):
        [kwargs.pop(key,None) for key in exclude_excel]
        data = pd.read_excel(src_path,**kwargs)
    elif src_path.lower().endswith('.csv'):
        [kwargs.pop(key,None) for key in excl_csv]
        kwargs['encoding'] = self.judge_coding(src_path)
        data = pd.read_csv(src_path,**kwargs)
    else:
        raise MesBadRequest("仅支持.xlsx 或 csv")
    return data

 

posted @ 2022-03-11 17:44  Οo白麒麟оΟ  阅读(50)  评论(0编辑  收藏  举报