localfile.py 文件源码

python
阅读 30 收藏 0 点赞 0 评论 0

项目:parade 作者: bailaohe 项目源码 文件源码
def export(df, table, export_type='excel', target_path=None, if_exists='replace', suffix=None):
        if target_path:
            if export_type == 'excel' and not suffix:
                suffix = 'xlsx'
            target_file = os.path.join(target_path,
                                       table + '-' + str(datetime.date.today())) + '.' + str(suffix or export_type)
            if if_exists == 'replace' and os.path.exists(target_file):
                os.remove(target_file)
            export_io = target_file

        else:
            export_io = BytesIO()

        if export_type == 'excel':
            writer = pd.ExcelWriter(export_io, engine='xlsxwriter')
            df.to_excel(writer, index=False)
            writer.save()
        elif export_type == 'csv':
            export_io = BytesIO(df.to_csv(target_path, index=False, chunksize=4096).encode())
        elif export_type == 'json':
            export_io = BytesIO(df.to_json(target_path, orient='records').encode())
        elif export_type == 'pickle':
            pkl.dump(df, export_io, protocol=pkl.HIGHEST_PROTOCOL)
        else:
            raise NotImplementedError("export type {} is not supported".format(export_type))
        return export_io, table + '.' + str(suffix or export_type)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号