Skip to content

Commit 4c9bee8

Browse files
committed
Pickle
1 parent 616cef5 commit 4c9bee8

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

README.md

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -951,12 +951,27 @@ db.commit()
951951
Pickle
952952
------
953953
```python
954-
>>> import pickle
955-
>>> P = collections.namedtuple('P', 'x y')
956-
>>> points = [P(0, 0), P(1, 1), P(2, 2)]
957-
>>> pickle.dump(points, open('points.p', 'wb'))
958-
>>> pickle.load(open('points.p', 'rb'))
959-
[P(x=0, y=0), P(x=1, y=1), P(x=2, y=2)]
954+
import pickle
955+
```
956+
957+
### Serialization
958+
```python
959+
<bytes> = pickle.dumps(<object>)
960+
<object> = pickle.loads(<bytes>)
961+
```
962+
963+
### Read Object from File
964+
```python
965+
def read_pickle_file(filename):
966+
with open(filename, 'rb') as file:
967+
return pickle.load(file)
968+
```
969+
970+
### Write Object to File
971+
```python
972+
def write_to_pickle_file(filename, an_object):
973+
with open(filename, 'wb') as file:
974+
pickle.dump(an_object, file)
960975
```
961976

962977

0 commit comments

Comments
 (0)