What causes the error "_pickle.UnpicklingError: invalid download key", "."?

I am trying to store 5000 data items in an array. These 5000 items are stored in an existing file (therefore it is not empty).

But I get an error message, and I don't know what causes it.

IN:

def array():

    name = 'puntos.df4'

    m = open(name, 'rb')
    v = []*5000

    m.seek(-5000, io.SEEK_END)
    fp = m.tell()
    sz = os.path.getsize(name)

    while fp < sz:
        pt = pickle.load(m)
        v.append(pt)

    m.close()
    return v

OUT:

line 23, in array
pt = pickle.load(m)
_pickle.UnpicklingError: invalid load key, ''.
+4
source share
3 answers

, . , , pickle , ... , . .., . , , . dump, load .

, dump , load load. , dump (, ), load ... load , . load . , , dict ( ), , klepto, dict .

pickle?

+5

, , , pickle:

# 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]

, , .

, , -5000, , , , .

, , .

+1

, , gzip.

, ,

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

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

But, if the pickle file is opened with gzip, everything is harmonious

with gzip.open('test.pklz', 'rb') as ifp:
    print(pickle.load(ifp))

[1, 2, 3]
+1
source

Source: https://habr.com/ru/post/1611048/


All Articles