Read Yaml File to Get the Diectionary Key

The full class of YAML is Even so Another Mark-up Linguistic communication. This file format is very pop now to store serialized information that is human being-readable. Information technology is mainly used for configuration files, only it can be used for other purposes also. Different types of scalar information such as number, string, etc., and compound data such as listing, the dictionary can exist the content of this file. The extension of this fie is '.yaml'. Multiple modules exist in Python to read the YAML file. The use of the PyYAML module to read the YAML file in Python has shown in this tutorial.

Pre-requisites:

Install the PyYAML module

PyYAML is the best module of Python to read the YAML file. PyYAML module is not installed with Python by default. Then, you take to install this package before checking the examples of this tutorial. Run the post-obit command to install PyYAML.

Create a YAML file

Create a YAML file named client.yaml with the following content to use this file in the next part of this tutorial.

client.yaml

- proper noun: Kamal Hossain

email: kamal@gmail.com

  mobile: 01843456790

  - name: Sakil Ahamed

email: sakil@gmail.com

  mobile: 015662343423

  - proper noun: Mizanur Rahman

email: mizan@gmail.com

  mobile: 01936784534

Case-i: Read the YAML content later on Converting a python object

After installing the PyYAML parcel, the YAML module can be imported into the python script to read YAML content by converting a python object.  The dump() function of the yaml module is used to create the YAML content by serializing the content of the python object. Create a python file with the following script to generate and impress the YAML stream by converting the content of the python object. The dump() office sorts the content of the dictionary based on the keys by default.

# Import YAML module

import yaml

# Declare a python object with information

books = [ { 'proper noun': 'Think Python: An Introduction to Software Design' , 'author': 'Allen B. Downey' , 'price': '23' } ,

{ 'name': 'Fluent Python: Clear, Concise, and Effective Programming' , 'author': 'Luciano Ramalho' , 'price': '50' } ,

{ 'name': 'Call up Python: An Introduction to Software Design' , 'author': 'Allen B. Downey' , 'price': '33' }

]

# Convert and print the JSON information in YAML stream

print (yaml.dump (books) )

Output:

The following output volition appear subsequently executing the above script. The items of each lexicon of the python list have converted into each fellow member of the YAML content. The content of the output has sorted based on the keys of the lexicon. For this, the value of the author fundamental has been printed showtime, and the value of the price key has been printed terminal.

Example-ii: Read the YAML content from a YAML file

The client.yaml file created in the previous function of this tutorial has been used in this example. Create a python file with the following script to read the sorted content of the client.yaml file based on the keys. The load() function has used in the script to read the total content of the client.yaml file. This function will return the content of the file every bit a python listing of dictionaries. Next, the dump() role is used to convert the listing into a YAML stream that has been printed subsequently.

# Import YAML module

import yaml

# Load YAML information from the file

with open ( 'client.yaml' ) as fh:

    read_data = yaml.load (fh, Loader=yaml.FullLoader )

# Print YAML data before sorting

print (read_data)

# Sort YAML data based on keys

sorted_data = yaml.dump (read_data)

# Impress YAML information after sorting

print (sorted_data)

Output:

The following output volition appear after executing the above script. Subsequently converting the content of the customer.yaml file into a python list of dictionaries, each dictionary of the python list has converted into each member of the YAML content like the previous example. The value of the sort_key parameter of the dump() function is set to True by default. And so, the output shows the sorted YAML content based on the keys.

Instance-3: Read the keys and values from a YAML file

Create a python file with the following script to read and print the fundamental and value separately from the client.yaml file. After loading the file's content into the read_data variable, the detail() role has used to read each fundamental and the corresponding value from the content. The nested 'for' loop has used to iterate the full content of the file and print the cardinal-value pairs.

# Import YAML module

import yaml

# Load the YAML file

with open up ( 'client.yaml' ) as fh:

# Load YAML data from the file

    read_data = yaml.load (fh, Loader=yaml.FullLoader )

# Iterate the loop to read and print YAML data

for i in range ( 0 , len (read_data) ):

for key, value in read_data[i].items ( ):

impress (fundamental, ":" , value)

print ( '' )

Output:

The post-obit output will appear after executing the above script. The file'southward content has non been sorted because the dump() function has not been used in the script.

Case-four: Read the YAML content into a list of dictionaries

The safe_load() function is used to catechumen the content of the YAML file into the python listing of the dictionaries. This function can exist used to load information from untrusted sources also.  Create a python file with the following script to load the content of a YAML file using the safe_load() function and print the loaded content.

# Import YAML module

import yaml

# Load the YAML file

with open ( 'customer.yaml' ) every bit fh:

# Catechumen the YAML data into a lexicon

    dictionary_data = yaml.safe_load (fh)

# Impress the dictionary data

print (dictionary_data)

Output:

The post-obit output will announced afterward executing the to a higher place script. A listing of dictionaries has been printed in the output.

Conclusion:

The means to read YAML content from a python object and a file take been shown in this tutorial by using various examples. The concept of parsing the YAML file using the PyYAML package will be cleared for the python users after practicing the examples of this tutorial.

Near the author

I am a trainer of spider web programming courses. I similar to write article or tutorial on diverse IT topics. I accept a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.

neelyfrours.blogspot.com

Source: https://linuxhint.com/read-yaml-file-in-python/

0 Response to "Read Yaml File to Get the Diectionary Key"

إرسال تعليق

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel