How to unpickle a file that has been hosted in a w

2019-07-22 03:36发布

问题:

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')) 


标签: python pickle