Questions tagged [pickle]

An object serialization module for Python. Use this tag together with the Python tag for questions related to storing or loading objects with Pickle.

Pickle provides a powerful set of tools for serializing and de-serializing Python objects.

4130 questions
469
votes
10 answers

How can I use pickle to save a dict?

I have looked through the information that the Python docs give, but I'm still a little confused. Could somebody post sample code that would write a new file then use pickle to dump a dictionary into it?
Chachmu
  • 5,558
  • 6
  • 26
  • 35
289
votes
8 answers

Python multiprocessing PicklingError: Can't pickle

I am sorry that I can't reproduce the error with a simpler example, and my code is too complicated to post. If I run the program in IPython shell instead of the regular Python, things work out well. I looked up some previous notes on this problem.…
Vendetta
  • 13,178
  • 19
  • 71
  • 111
289
votes
2 answers

Using pickle.dump - TypeError: must be str, not bytes

I'm using python3.3 and I'm having a cryptic error when trying to pickle a simple dictionary. Here is the code: import os import pickle from pickle import * os.chdir('c:/Python26/progfiles/') def storvars(vdict): f =…
John Rowland
  • 3,069
  • 2
  • 13
  • 7
288
votes
5 answers

Saving an Object (Data persistence)

I've created an object like this: company1.name = 'banana' company1.value = 40 I would like to save this object. How can I do that?
Peterstone
  • 5,777
  • 13
  • 37
  • 48
246
votes
9 answers

Storing Python dictionaries

I'm used to bringing data in and out of Python using CSV files, but there are obvious challenges to this. Are there simple ways to store a dictionary (or sets of dictionaries) in a JSON or pickle file? For example: data = {} data ['key1'] =…
mike
  • 19,373
  • 28
  • 72
  • 93
225
votes
12 answers

Can't pickle when using multiprocessing Pool.map()

I'm trying to use multiprocessing's Pool.map() function to divide out work simultaneously. When I use the following code, it works fine: import multiprocessing def f(x): return x*x def go(): pool = multiprocessing.Pool(processes=4) …
ventolin
  • 2,843
  • 3
  • 16
  • 24
215
votes
16 answers

Serializing class instance to JSON

I am trying to create a JSON string representation of a class instance and having difficulty. Let's say the class is built like this: class testclass: value1 = "a" value2 = "b" A call to the json.dumps is made like this: t =…
ferhan
  • 2,211
  • 2
  • 12
  • 9
190
votes
18 answers

Multiprocessing: How to use Pool.map on a function defined in a class?

When I run something like: from multiprocessing import Pool p = Pool(5) def f(x): return x*x p.map(f, [1,2,3]) it works fine. However, putting this as a function of a class: class calculate(object): def run(self): def f(x): …
Mermoz
  • 12,738
  • 16
  • 53
  • 81
169
votes
7 answers

Pickle incompatibility of numpy arrays between Python 2 and 3

I am trying to load the MNIST dataset linked here in Python 3.2 using this program: import pickle import gzip import numpy with gzip.open('mnist.pkl.gz', 'rb') as f: l = list(pickle.load(f)) print(l) Unfortunately, it gives me the…
Neil G
  • 28,787
  • 31
  • 143
  • 234
146
votes
7 answers

Saving and loading objects and using pickle

I´m trying to save and load objects using pickle module. First I declare my objects: >>> class Fruits:pass ... >>> banana = Fruits() >>> banana.color = 'yellow' >>> banana.value = 30 After that I open a file called 'Fruits.obj'(previously I…
Peterstone
  • 5,777
  • 13
  • 37
  • 48
144
votes
8 answers

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

I am getting an interesting error while trying to use Unpickler.load(), here is the source code: open(target, 'a').close() scores = {}; with open(target, "rb") as file: unpickler = pickle.Unpickler(file); scores = unpickler.load(); if…
Magix
  • 3,557
  • 3
  • 18
  • 43
142
votes
2 answers

Unpickling a python 2 object with python 3

I'm wondering if there is a way to load an object that was pickled in Python 2.4, with Python 3.4. I've been running 2to3 on a large amount of company legacy code to get it up to date. Having done this, when running the file I get the following…
NDevox
  • 3,751
  • 4
  • 18
  • 32
141
votes
6 answers

best way to preserve numpy arrays on disk

I am looking for a fast way to preserve large numpy arrays. I want to save them to the disk in a binary format, then read them back into memory relatively fastly. cPickle is not fast enough, unfortunately. I found numpy.savez and numpy.load. But…
Vendetta
  • 13,178
  • 19
  • 71
  • 111
137
votes
9 answers

Common use-cases for pickle in Python

I've looked at the pickle documentation, but I don't understand where pickle is useful. What are some common use-cases for pickle?
satoru
  • 27,201
  • 27
  • 83
  • 126
135
votes
7 answers

Pickle or json?

I need to save to disk a little dict object whose keys are of the type str and values are ints and then recover it. Something like this: {'juanjo': 2, 'pedro':99, 'other': 333} What is the best option and why? Serialize it with pickle or with…
Juanjo Conti
  • 25,163
  • 37
  • 101
  • 128
1
2 3
99 100