如何使用project-lib python将二进制文件保存到我的项目资产中? [英] How can I save a binary file to my project assets using project-lib python?

查看:174
本文介绍了如何使用project-lib python将二进制文件保存到我的项目资产中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

项目库文档显示了如何将熊猫数据框保存到项目资产:

The project lib documentation shows how to save a pandas dataframe to the project assets:

 # Import the lib
 from project_lib import Project
 project = Project(sc,"<ProjectId>", "<ProjectToken>")

 # let's assume you have the pandas DataFrame  pandas_df which contains the data
 # you want to save in your object storage as a csv file
 project.save_data("file_name.csv", pandas_df.to_csv())

 # the function returns a dict which contains the asset_id, bucket_name and file_name
 # upon successful saving of the data

但是,如果我有本地文件...

However, if I have a local file ...

! wget url_to_binary_file

然后如何将该文件上传到项目资产?

How can I then upload that file to the project’s assets?

推荐答案

我需要以字节为单位读取文件.请注意,这会将文件读入内存,请不要尝试这样做,因为您的文件大于可用内存:

I needed to read the file as bytes. Note that this will read the file into memory, don’t try this is you have a file that is larger than your available memory:

import io

filename = ‘thefilename’
with open(filename, 'rb') as z:
        data = io.BytesIO(z.read())
        project.save_data(
            filename, data, set_project_asset=True, overwrite=True
        )

这篇关于如何使用project-lib python将二进制文件保存到我的项目资产中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆