python - Cannot load pickled object -


the problem having when try load pickled object. have tried using both pickle.loads , pickle.load here results:

pickle.loads - typeerror: 'str' not support buffer interface

pickle.load - typeerror: file must have 'read' , 'readline' attributes

can please tell me doing wrong in process? thanks, , here code:

elif str(parser) == 'swisswithdrawn_parser':        # swissprot name changes        print('gathering swissprot update info...')        cache_hits = 0        cache_misses = 0        files = set()         f in os.listdir('out/cache/'):            if os.path.isfile('out/cache/'+f):                files.add(f)         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))             ################### testing - 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)             # contents returned httpresponse object            content_list = [x.decode().strip() x in content.readlines()]            if not cached:                open('out/cache/'+hashed_url, 'wb') fp:                    pickle.dump(content_list, fp)            ####################################################################             # no replacement            if len(content_list) 0:                change_log['swiss-names'] =                    { name : 'withdrawn' }            # new name            else:                new_name = content_list[1]                change_log['swiss-names'] =                   { name : new_name } 

you need either read file first (as binary bytes) , use pickle.loads(), or pass open file object pickle.load() command. latter preferable:

with open('out/cache/' +hashed_url, 'rb') pickle_file:     content = pickle.load(pickle_file) 

neither method supports loading pickle filename.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -