How can I save a binary file to my project assets using project-lib python?

守給你的承諾、 提交于 2019-12-25 00:57:08

问题


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?


回答1:


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
        )


来源:https://stackoverflow.com/questions/51666863/how-can-i-save-a-binary-file-to-my-project-assets-using-project-lib-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!