What causes the error “_pickle.UnpicklingError: invalid load key, ' '.”?

前端 未结 5 2222
清歌不尽
清歌不尽 2020-12-08 13:49

I\'m trying to storage 5000 data elements on an array. This 5000 elements are storage on an existent file (therefore it\'s not empty).

But I\'m getting an error an

5条回答
  •  感动是毒
    2020-12-08 14:25

    I am not completely sure what you're trying to achieve by seeking to a specific offset and attempting to load individual values manually, the typical usage of the pickle module is:

    # save data to a file
    with open('myfile.pickle','wb') as fout:
        pickle.dump([1,2,3],fout)
    
    # read data from a file
    with open('myfile.pickle') as fin:
        print pickle.load(fin)
    
    # output
    >> [1, 2, 3]
    

    If you dumped a list, you'll load a list, there's no need to load each item individually.

    you're saying that you got an error before you were seeking to the -5000 offset, maybe the file you're trying to read is corrupted.

    If you have access to the original data, I suggest you try saving it to a new file and reading it as in the example.

提交回复
热议问题