slideWXY
slideWXY

Reputation: 1157

Add a new key in a pickle file

I want to store data by an iterative way.

how could I add some new keys in an pickle that contain already a dict ??

For example

with open(filename_directory, 'wb') as f:
    pickle.dump({"a" : 1, "b":2 }, f)

new_dict_ = {}
for i in range(10):
    new_dict_ [i] = i

with open(filename_directory, 'a') as f:
    pickle.dump( update_dict_with_my( new_dict_ ) , f)

Upvotes: 3

Views: 1667

Answers (1)

Jean-François Fabre
Jean-François Fabre

Reputation: 140186

It makes no sense to append to a pickle file. The pickle format is one binary structure, and if you append to a file containing one structure, at best the rest is ignored at worst you corrupt the existing data.

(it makes not more sense to append to a json file either by the way: in both cases, the new data must be inserted in the existing structure, not appended)

You could:

  • read back the dictionary
  • update it with the new values (or the other way round, it has a different effect depending on if there are some shared keys)
  • dump it back to disk

like this:

with open(filename_directory, 'wb') as f:
    pickle.dump({"a" : 1, "b":2 }, f)

new_dict_ = {i:i for i in range(10)}

with open(filename_directory, 'rb') as f:
    new_dict_.update(pickle.load(f))

with open(filename_directory, 'wb') as f:
    pickle.dump( new_dict_ , f)

to read/update/dump with the new keys being on top priority, read first, update and dump:

with open(filename_directory, 'rb') as f:
    new_dict_ = update(pickle.load(f))
for i in range(10):
    new_dict_ [i] = i

with open(filename_directory, 'wb') as f:
    pickle.dump( new_dict_ , f)

note that you may be better off with json format if you're planning to serialize basic types in a dictionary. At least the dumped format is readable and even updatable by hand and as noted above, if you try to append to a json file, it doesn't work either, even if it's a text file. There will be at least the ] or } final char that needs moving, adding a coma, etc... forget it).

Upvotes: 2

Related Questions