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

前端 未结 5 2206
清歌不尽
清歌不尽 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:24

    This may not be relevant to your specific issue, but I had a similar problem when the pickle archive had been created using gzip.

    For example if a compressed pickle archive is made like this,

    import gzip, pickle
    with gzip.open('test.pklz', 'wb') as ofp:
        pickle.dump([1,2,3], ofp)
    

    Trying to open it throws the errors

     with open('test.pklz', 'rb') as ifp:
         print(pickle.load(ifp))
    Traceback (most recent call last):
      File "", line 2, in 
    _pickle.UnpicklingError: invalid load key, ''.
    

    But, if the pickle file is opened using gzip all is harmonious

    with gzip.open('test.pklz', 'rb') as ifp:
        print(pickle.load(ifp))
    
    [1, 2, 3]
    

提交回复
热议问题