无法加载腌制对象

Hou*_*ini 48 python pickle python-3.x

我遇到的问题是当我尝试加载pickle对象时.我尝试过使用两者pickle.loads,pickle.load结果如下:

pickle.loads - pickle.loads

pickle.load - pickle.load

有谁能告诉我在这个过程中我做错了什么?谢谢,这是我的代码:

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 }
Run Code Online (Sandbox Code Playgroud)

Mar*_*ers 67

您需要先读取文件(作为二进制文件bytes)并使用pickle.loads(),或者将打开的文件对象传递给pickle.load()命令.后者是优选的:

with open('out/cache/' +hashed_url, 'rb') as pickle_file:
    content = pickle.load(pickle_file)
Run Code Online (Sandbox Code Playgroud)

这两种方法都不支持从文件名加载pickle.


小智 9

如果您碰巧将 python2 移植到 3 并遇到此错误,则 python2 和 3 处理字节不同,导致需要使用“b”选项打开文件句柄。例如,在 python2 中open(file, 'r') as f: my_list = pickle.load(f)可以,但在 python3 中则不行。相反,你必须打开open(file, 'rb') as f: my_list = pickle.load(f)