Cannot load pickled object












27















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? Thanks, and here is my code:



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 }









share|improve this question





























    27















    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? Thanks, and here is my code:



    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 }









    share|improve this question



























      27












      27








      27


      2






      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? Thanks, and here is my code:



      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 }









      share|improve this question
















      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? Thanks, and here is my code:



      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 python-3.x pickle






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 15 '13 at 21:13









      Martijn Pieters

      702k13224392274




      702k13224392274










      asked Aug 15 '13 at 21:09









      HoudiniHoudini

      1,06342041




      1,06342041
























          1 Answer
          1






          active

          oldest

          votes


















          47














          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.






          share|improve this answer


























          • Thank you, thanks to your answer I was able to finally get my first implementation of a cache working! :)

            – Houdini
            Aug 15 '13 at 21:33













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18261898%2fcannot-load-pickled-object%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          47














          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.






          share|improve this answer


























          • Thank you, thanks to your answer I was able to finally get my first implementation of a cache working! :)

            – Houdini
            Aug 15 '13 at 21:33


















          47














          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.






          share|improve this answer


























          • Thank you, thanks to your answer I was able to finally get my first implementation of a cache working! :)

            – Houdini
            Aug 15 '13 at 21:33
















          47












          47








          47







          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.






          share|improve this answer















          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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 12 '16 at 7:55

























          answered Aug 15 '13 at 21:13









          Martijn PietersMartijn Pieters

          702k13224392274




          702k13224392274













          • Thank you, thanks to your answer I was able to finally get my first implementation of a cache working! :)

            – Houdini
            Aug 15 '13 at 21:33





















          • Thank you, thanks to your answer I was able to finally get my first implementation of a cache working! :)

            – Houdini
            Aug 15 '13 at 21:33



















          Thank you, thanks to your answer I was able to finally get my first implementation of a cache working! :)

          – Houdini
          Aug 15 '13 at 21:33







          Thank you, thanks to your answer I was able to finally get my first implementation of a cache working! :)

          – Houdini
          Aug 15 '13 at 21:33




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18261898%2fcannot-load-pickled-object%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Contact image not getting when fetch all contact list from iPhone by CNContact

          count number of partitions of a set with n elements into k subsets

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks