The problem I am having is when I try to load the pickled object. I have tried using both pickle.loads
and pickle.load
Here are the results:
pickle.loads
:
TypeError: 'str' does not support the buffer interface
pickle.load
:
TypeError: file must have 'read' and 'readline' attributes
Can someone please tell me what I am doing wrong in this process?
elif str(parser) == "SwissWithdrawn_Parser": # swissprot name changes print("Gathering SwissProt update info...") cache_hits = 0 cache_misses = 0 files = set() for f in os.listdir("out/cache/"): if os.path.isfile("out/cache/" + f): files.add(f) for name in sp_lost_names: cached = False url = ( "http://www.uniprot.org/uniprot/?query=mnemonic%3a" + name + "+active%3ayes&format=tab&columns=entry%20name" ) hashed_url = str(hash(url)) ################### For Testing Only - use cache ################## if hashed_url in files: cached = True cache_hits += 1 content = pickle.loads("out/cache/" + hashed_url) # <-- problematic line else: cache_misses += 1 content = urllib.request.urlopen(url) # get the contents returned from the HTTPResponse object content_list = [x.decode().strip() for x in content.readlines()] if not cached: with open("out/cache/" + hashed_url, "wb") as fp: pickle.dump(content_list, fp) #################################################################### # no replacement if len(content_list) is 0: change_log["swiss-names"] = {name: "withdrawn"} # get the new name else: new_name = content_list[1] change_log["swiss-names"] = {name: new_name}
Python Pickle load You have to use pickle. load() function to do that. The primary argument of pickle load function is the file object that you get by opening the file in read-binary (rb) mode. Simple!
The process of loading a pickled file back into a Python program is similar to the one you saw previously: use the open() function again, but this time with 'rb' as second argument (instead of wb ). The r stands for read mode and the b stands for binary mode. You'll be reading a binary file. Assign this to infile .
Consider passing allow_pickle=False to load data that is known not to contain object arrays for the safer handling of untrusted sources. In that case, try saving just the numpy array as np. save(filename, x[0]) . This will not use any pickling to save your data and resolves the issue.
First, import pickle to use it, then we define an example dictionary, which is a Python object. Next, we open a file (note that we open to write bytes in Python 3+), then we use pickle. dump() to put the dict into opened file, then close. Use pickle.
You need to either read the file first (as binary bytes
) and use pickle.loads()
, or pass an open file object to the pickle.load()
command. The latter is preferable:
with open('out/cache/' +hashed_url, 'rb') as pickle_file: content = pickle.load(pickle_file)
Neither method supports loading a pickle from a filename.
If you happen to be porting python2 to 3 and run into this error, python2 and 3 handle bytes different leading to the requirement to open your file handle with the 'b' option. For instance in python2 open(file, 'r') as f: my_list = pickle.load(f)
works , but not in python3. Instead you must open with open(file, 'rb') as f: my_list = pickle.load(f)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With