Friday, December 27, 2024
Google search engine
HomeLanguagesParse a YAML file in Python

Parse a YAML file in Python

YAML is the abbreviation of Yet Another Markup Language or YAML ain’t markup Language which is the data format used to exchange data. YAML can store only data and no commands. It is similar to the XML and JSON data formats. In this article, we will dive deep into the concept of parsing YAML files in Python along with the example.

Parsing YAML Files in Python

PyYAML Module in Python Programming is considered as the Parser for Python. Using this library, we can perform different operations on the YAML files like reading or writing the YAML files. Serialization and Persisting YAML data can also be done using this PyYAML Module in Python.

For the usage of the PyYAML Module, we need to install it in Python by executing the below command:

pip install pyyaml

YAML files are saved using 2 different extensions, that are, .yaml and .yml. As we need to parse the YAML File, so we have created two YAML files which consist of data in the Key: Value pair. We will be taking the following YAML files as an example for our article.

neveropenforgeek.yml

UserName: neveropen
Password: GFG@123
Phone: 1234567890
Website: geeksforgeeks.org
Skills:
-Python
-SQL
-Django
-Javascript

multi_docs.yml

---
UserName: neveropen
Password: GFG@123
Phone: 1234567890
Website: geeksforgeeks.org
Skills:
-Python
-SQL
-Django
-Javascript
...
---
UserName: Google
Password: google@123
Phone: 1234567890
Website: google.com
Skills:
-Python
-SQL
-Django
-Javascript
...
---
UserName: Yahoo
Password: yahoo@123
Phone: 1234567890
Website: yahoo.com
Skills:
-Python
-SQL
-Django
-Javascript

Now let us see a few methods using which we can parse a YAML file in Python.

Using load() Function

The yaml.load() function in the YAML module is used to read the YAML data or object into a Python Dictionary. There can be YAML data that consists of a huge number of key-value pairs (configuration files), so to read these files, the load() function can be helpful as it performs the deserialization of YAML data into Python. The Loader parameter of the load() function is set to SafeLoader, which intends to load the data of YAML safely. This is helpful in scenarios where the input is taken from untrusted sources.

Python3




import yaml
with open('neveropen.yml', 'r') as f:
    data = yaml.load(f, Loader=yaml.SafeLoader)
     
# Print the values as a dictionary
print(data)


Output:

{'UserName': 'neveropen', 'Password': 'GFG@123', 'Phone': 1234567890, 
'Website': 'geeksforgeeks.org', 'Skills': '-Python -SQL -Django -Javascript'}

Using full_load() Function

The yaml.full_load() function is used to parse the content of the YAML file in the form of key-value pairs. Then using the Python get() method, we can get specific data from the YAML file.

Python3




import yaml
with open('neveropen.yml', 'r') as f:
    data = yaml.full_load(f)
     
# Print the values as a dictionary
output = {
    'UserName': data.get('UserName'),
    'Password': data.get('Password'),
    'phone': data.get('Phone'),
    'Skills': ' '.join(data.get('Skills', []))
}
 
print(output)


Output:

{'UserName': 'neveropen', 'Password': 'GFG@123', 'phone': 1234567890, 
'Skills': '- P y t h o n - S Q L - D j a n g o - J a v a s c r i p t'}

Using safe_load() Function

Another way to load the YAML file in Python is by using the safe_load() method. It can be used in place of the load() method’s SafeLoader parameter, when the data is loaded from an untrusted source.

Python3




import yaml
 
with open('neveropen.yml') as f:
    dict = yaml.safe_load(f)
    print(dict)


Output:

{'UserName': 'neveropen', 'Password': 'GFG@123 *', 'phone': 987909890, 
'website': 'geeksforgeeks.org',
'Skills': '-Python -SQL -Django -Rest Framework -JavaScript'}

Using load_all() Function

The load_all() method is used when we want to load a YAML document present in a single file. The Loader parameter of the load() function is set to SafeLoader, which intends to load the data of YAML safely. This is helpful in scenarios where the input is taken from untrusted sources.

Python3




import yaml
 
from yaml.loader import SafeLoader
# open yaml file in read
 
with open('multiple_documents.yml', 'r') as f:
    yaml_data = list(yaml.load_all(f, Loader=SafeLoader))
    print(yaml_data)


Output:

[{'UserName': 'neveropen', 'Password': 'GFG@123', 'Phone': 1234567890, 
'Website': 'geeksforgeeks.org', 'Skills': '-Python -SQL -Django -Resst Framework -Javascript'},
{'UserName': 'Google', 'Password': 'google@123', 'Phone': 1234567890,
'Website': 'google.com', 'Skills': '-Python -SQL -Django -Resst Framework -Javascript'},
{'UserName': 'Yahoo', 'Password': 'yahoo@123', 'Phone': 1234567890,
'Website': 'yahoo.com', 'Skills': '-Python -SQL -Django -Resst Framework -Javascript'}]

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments