Skip to content

Commit dca7228

Browse files
committed
Update YAML example
1 parent 5aac6bc commit dca7228

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

08 File Operations/4 Saving and loading Python objects.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Fortunately, Python comes with a number of modules that make this task much easi
1313
```python
1414
import pickle
1515

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
1717
data = {'a': [1,2,3], 'b': "Ala has a cat", 'c': 123}
1818

1919
with open ('data. pickle', 'wb') as save_file:
@@ -32,21 +32,32 @@ with open ('data.pickle', 'rb') as read_file:
3232
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.
3333

3434
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}
3539

40+
with open('data.json', 'w') as saved_file:
41+
json.dump(data, saved_file)
42+
43+
44+
with open('data.json', 'r') as read_file:
45+
print(json.load(read_file))
46+
```
47+
or
3648
```python
3749
import yaml
3850

39-
# The variable data contains a dictionary of the retrieved data that we want to save the
4051
data = {'a': [1,2,3], 'b': "Ala ma cat", 'c': 123}
4152

4253
with open('data.yaml', 'w') as saved_file:
4354
yaml.dump(data, saved_file)
4455

4556

4657
with open('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
4859
```
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.
5061

5162
***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.
5263

0 commit comments

Comments
 (0)