Python pickle/unpickle a list to/from a file

前端 未结 2 1756
我在风中等你
我在风中等你 2020-12-11 14:43

I have a list that looks like this:

a = [[\'a string\', [0, 0, 0], [22, \'bee sting\']], [\'see string\', 
    [0, 2, 0], [22, \'d string\']]]
相关标签:
2条回答
  • 2020-12-11 15:06

    To add to @ Rapolas K's answer:

    I found that I had problems with the file not closing so used this method:

    with open('afile','rb') as f:
         pickle.load(f)
    
    0 讨论(0)
  • 2020-12-11 15:33

    Decided to make it as an answer. pickle.load method expects to get a file like object, but you are providing a string instead, and therefore an exception. So instead of:

    pickle.load('afile')
    

    you should do:

    pickle.load(open('afile', 'rb'))
    
    0 讨论(0)
提交回复
热议问题