pip install pyyaml
app.py
1 2 3 4 5 6 7 8 9 10 |
# process_yaml.py file import yaml with open(r'file.yaml') as file: # The FullLoader parameter handles the conversion from YAML # scalar values to Python the dictionary format fruits_list = yaml.load(file, Loader=yaml.FullLoader) print(fruits_list) |
file.yaml
1 2 3 4 5 |
apples: 20 mangoes: 2 bananas: 3 grapes: 100 pineapples: 1 |
Write YAML Files
app.py
1 2 3 4 5 6 7 |
import yaml dict_file = [{'sports' : ['soccer', 'football', 'basketball', 'cricket', 'hockey', 'table tennis']}, {'countries' : ['Pakistan', 'USA', 'India', 'China', 'Germany', 'France', 'Spain']}] with open(r'file.yaml', 'w') as file: documents = yaml.dump(dict_file, file) |