We’ve already covered .ini, .json and .xml in below articles, in this article we’ll focus on .yaml config files.
YAML or YAML Ain't Markup Language is a case sensitive and human-friendly data serialization language used mainly for configurations.
For reading and writing to the YAML file, we first need to install the PyYAML package by using the following command.
$ pip install pyyamlLet’s now dive into read and write operations to the YAML file.
We can write data to the YAML file using yaml.dump() method which takes python object and file pointer as parameters.
In the below example, we are writing the dictionary article_info to the YAML file tutswiki.yaml.
import yaml
article_info = [
{
'Details': {
'domain' : 'www.tutswiki.com',
'language': 'python',
'date': '11/09/2020'
}
}
]
with open("tutswiki.yaml", 'w') as yamlfile:
data = yaml.dump(article_info, yamlfile)
print("Write successful")Output:
Write successful
Process finished with exit code 0File: tutswiki.yaml
- Details:
date: 11/09/2020
domain: www.tutswiki.com
language: pythonNote:
sort_keysparameter indump()method can be used to sort the keys in ascending order.
sorted = yaml.dump(data, sort_keys = True)We can read the data using yaml.load() method which takes file pointer and Loader as parameters.
FullLoader handles the conversion from YAML scalar values to the Python dictionary.
To read the value from the file, we write the below code
import yaml
with open("tutswiki.yaml", "r") as yamlfile:
data = yaml.load(yamlfile, Loader=yaml.FullLoader)
print("Read successful")
print(data)Output:
Read successful
[{'Details': {'date': '11/09/2020', 'domain': 'www.tutswiki.com', 'language': 'python'}}]
Process finished with exit code 0Now if we want to read the value of domain, we would write
print(data[0]['Details']['domain'])The index [0] is used to select the tag we want to read. And we move further using the key values.
Output:
www.tutswiki.comWhat if we want to update the language python to java? For that, we will have to first read the file, reach to the key language and update its value and write the data to the file as done below.
import yaml
article_info = [
{
'Details': {
'domain' : 'www.tutswiki.com',
'language': 'python',
'date': '11/09/2020'
}
}
]
with open("tutswiki.yaml", "r") as yamlfile:
data = yaml.load(yamlfile, Loader=yaml.FullLoader)
print(data)
print("Read successful")
data[0]['Details']['language'] = 'java'
print("Value of language updated from 'python' to 'java'")
yamlfile.close()
with open("tutswiki.yaml", 'w') as yamlfile:
data1 = yaml.dump(data, yamlfile)
print(data)
print("Write successful")
yamlfile.close()Output:
[{'Details': {'date': '11/09/2020', 'domain': 'www.tutswiki.com', 'language': 'python'}}]
Read successful
Value of language updated from 'python' to 'java'
[{'Details': {'date': '11/09/2020', 'domain': 'www.tutswiki.com', 'language': 'java'}}]
Write successful
Process finished with exit code 0File: tutswiki.yaml
- Details:
date: 11/09/2020
domain: www.tutswiki.com
language: javaAs you can see, language in tutswiki.yaml file has been updated successfully.
Help us improve this content by editing this page on GitHub