From 4c9bee873195cd7e484aa50f1ca218f37ced5316 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Wed, 30 Jan 2019 05:44:55 +0100 Subject: [PATCH] Pickle --- README.md | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 807a2e3..96ee8fc 100644 --- a/README.md +++ b/README.md @@ -951,12 +951,27 @@ db.commit() Pickle ------ ```python ->>> import pickle ->>> P = collections.namedtuple('P', 'x y') ->>> points = [P(0, 0), P(1, 1), P(2, 2)] ->>> pickle.dump(points, open('points.p', 'wb')) ->>> pickle.load(open('points.p', 'rb')) -[P(x=0, y=0), P(x=1, y=1), P(x=2, y=2)] +import pickle +``` + +### Serialization +```python + = pickle.dumps() + = pickle.loads() +``` + +### Read Object from File +```python +def read_pickle_file(filename): + with open(filename, 'rb') as file: + return pickle.load(file) +``` + +### Write Object to File +```python +def write_to_pickle_file(filename, an_object): + with open(filename, 'wb') as file: + pickle.dump(an_object, file) ```