How to unpickle a file that has been hosted in a web URL in python

烈酒焚心 提交于 2020-01-05 02:03:19

问题


The normal way to pickle and unpickle an object is as follows:

Pickle an object:

import cloudpickle as cp

cp.dump(objects, open("picklefile.pkl", 'wb'))

UnPickle an object: (load the pickled file):

loaded_pickle_object = cp.load(open("picklefile.pkl", 'rb'))

Now, what if the pickled object is hosted in a server, for example a google drive: I am not able to unpickle the object if I directly provide the URL of that object in the path. The following is not working:I get an IOERROR

UnPickle an object: (load the pickled file):

loaded_pickle_object = cp.load(open("https://drive.google.com/file/d/pickled_file", 'rb')) 

Can someone tell me how to load a pickled file into python that is hosted in a web URL?


回答1:


The following has worked for me when importing gdrive pickled files into a Python 3 colab:

from urllib.request import urlopen
loaded_pickle_object = cp.load(urlopen("https://drive.google.com/file/d/pickled_file", 'rb')) 


来源:https://stackoverflow.com/questions/50624042/how-to-unpickle-a-file-that-has-been-hosted-in-a-web-url-in-python

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