python - What causes the error "_pickle.UnpicklingError: invalid load key, ' '."? -


i'm trying storage 5000 data elements on array. 5000 elements storage on existent file (therefore it's not empty).

but i'm getting error , don't know causing 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, ''. 

pickling recursive, not sequential. thus, pickle list, pickle start pickle containing list, pickle first element… diving first element , pickling dependencies , sub-elements until first element serialized. moves on next element of list, , on, until finishes list , finishes serializing enclosing list. in short, it's hard treat recursive pickle sequential, except special cases. it's better use smarter pattern on dump, if want load in special way.

the common pickle, pickle single dump file -- have load @ once single load. however, if open file handle , multiple dump calls (e.g. 1 each element of list, or tuple of selected elements), load mirror that… open file handle , multiple load calls until have list elements , can reconstruct list. it's still not easy selectively load list elements, however. that, you'd have store list elements dict (with index of element or chunk key) using package klepto, can break pickled dict several files transparently, , enables easy loading of specific elements.

saving , loading multiple objects in pickle file?


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -