trying yaml

This commit is contained in:
2020-11-14 14:36:17 +01:00
parent 33cfef242b
commit 781970ab8c
4 changed files with 72 additions and 0 deletions
+20
View File
@@ -0,0 +1,20 @@
# categories.yaml file
sports:
- soccer
- football
- basketball
- cricket
- hockey
- table tennis
countries:
- Pakistan
- USA
- India
- China
- Germany
- France
- Spain
+10
View File
@@ -0,0 +1,10 @@
# fruits.yaml
apples: 20
mangoes: 2
bananas: 3
grapes: 100
pineapples: 1
machin: 23
truc: 21347
last: 2q348789
+27
View File
@@ -0,0 +1,27 @@
#!/usr/bin/env python
import yaml
# Reading Yaml
with open('fruits.yaml') as file:
fruits_dict = yaml.load(file, Loader=yaml.FullLoader)
print(type(fruits_dict['last']))
# print(fruits_dict)
# Note
# yaml.load(file, Loader=yaml.FullLoader) = yaml.full_load(file)
# with open('categories.yaml') as file:
# documents = yaml.full_load(file)
# for item, doc in documents.items():
# print(item, ":", doc)
# Writting Yaml
# dict_file = [
# {'sports' : ['soccer', 'football', 'basketball', 'cricket', 'hockey', 'table tennis']},
# {'countries' : ['Pakistan', 'USA', 'India', 'China', 'Germany', 'France', 'Spain']}
# ]
# with open("storing.yaml", 'w') as file:
# documents = yaml.dump(dict_file, file)
+15
View File
@@ -0,0 +1,15 @@
- sports:
- soccer
- football
- basketball
- cricket
- hockey
- table tennis
- countries:
- Pakistan
- USA
- India
- China
- Germany
- France
- Spain