You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 08 File Operations/4 Saving and loading Python objects.md
+15-4Lines changed: 15 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@ Fortunately, Python comes with a number of modules that make this task much easi
13
13
```python
14
14
import pickle
15
15
16
-
# The variable data contains a dictionary of the collected data that we want to save the
16
+
# The variable data contains a dictionary of the collected data that we want to save
17
17
data = {'a': [1,2,3], 'b': "Ala has a cat", 'c': 123}
18
18
19
19
withopen ('data. pickle', 'wb') as save_file:
@@ -32,21 +32,32 @@ with open ('data.pickle', 'rb') as read_file:
32
32
Pickle functions `dump` and `load` can be called several times for a given file. Then successive variables are written to it, which can be read one by one. However, in order to save the program settings, it is recommended to create one dictionary containing them all and save/load it once.
33
33
34
34
There are other modules that work similarly to `pickle`. For example `json` and `yaml` (the latter is not the standard Python module, but in the distribution of Anaconda is installed by default). Unlike the `pickle` module, they require files that are opened in text mode (without `b`). They will create text files in [JSON](https://en.wikipedia.org/wiki/JSON) or [YAML](https://en.wikipedia.org/wiki/YAML) formats that can be read and edited in external tools. At the same time, these formats can be read from Python:
35
+
```python
36
+
import json
37
+
38
+
data = {'a': [1,2,3], 'b': "Ala ma cat", 'c': 123}
35
39
40
+
withopen('data.json', 'w') as saved_file:
41
+
json.dump(data, saved_file)
42
+
43
+
44
+
withopen('data.json', 'r') as read_file:
45
+
print(json.load(read_file))
46
+
```
47
+
or
36
48
```python
37
49
import yaml
38
50
39
-
# The variable data contains a dictionary of the retrieved data that we want to save the
40
51
data = {'a': [1,2,3], 'b': "Ala ma cat", 'c': 123}
41
52
42
53
withopen('data.yaml', 'w') as saved_file:
43
54
yaml.dump(data, saved_file)
44
55
45
56
46
57
withopen('data.yaml', 'r') as read_file:
47
-
print(yaml.load(read_file))
58
+
print(yaml.safe_load(read_file))# Unlike pickle and json yaml uses safe_load instead of load
48
59
```
49
-
The above code works the same as for the module `pickle`. However, please open the file `data.yaml` in a text editor: its content should be readable and possible to modify manually.
60
+
The above codes work the same as for the module `pickle`. However, please open the file `data.yaml` in a text editor: its content should be readable and possible to modify manually.
50
61
51
62
***Attention!*** When using the `json` module, it is not possible to save a variable of any type (e.g. tuples which must first be converted into lists). `pickle` and `yaml` do not have this limitation.
0 commit comments