Skip to content Skip to sidebar Skip to footer

Read Yaml Files in Python Line by Line

The full class of YAML is All the same Another Marking-upward Language. This file format is very popular now to shop serialized data that is homo-readable. It is mainly used for configuration files, but information technology tin be used for other purposes also. Different types of scalar data such as number, string, etc., and compound data such as list, 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 all-time module of Python to read the YAML file. PyYAML module is not installed with Python by default. So, yous take to install this bundle before checking the examples of this tutorial. Run the following command to install PyYAML.

Create a YAML file

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

client.yaml

- name: Kamal Hossain

electronic mail: kamal@gmail.com

  mobile: 01843456790

  - proper name: Sakil Ahamed

electronic mail: sakil@gmail.com

  mobile: 015662343423

  - name: Mizanur Rahman

electronic mail: mizan@gmail.com

  mobile: 01936784534

Example-1: Read the YAML content after Converting a python object

Afterwards installing the PyYAML package, the YAML module tin 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() function sorts the content of the lexicon based on the keys by default.

# Import YAML module

import yaml

# Declare a python object with data

books = [ { 'name': 'Think Python: An Introduction to Software Pattern' , 'writer': 'Allen B. Downey' , 'price': '23' } ,

{ 'name': 'Fluent Python: Articulate, Concise, and Effective Programming' , 'author': 'Luciano Ramalho' , 'cost': 'fifty' } ,

{ 'name': 'Retrieve Python: An Introduction to Software Design' , 'author': 'Allen B. Downey' , 'cost': '33' }

]

# Catechumen and print the JSON data in YAML stream

print (yaml.dump (books) )

Output:

The following output volition appear after executing the in a higher place script. The items of each lexicon of the python list accept converted into each 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 key has been printed outset, and the value of the cost cardinal has been printed last.

Case-2: Read the YAML content from a YAML file

The client.yaml file created in the previous part of this tutorial has been used in this example. Create a python file with the following script to read the sorted content of the customer.yaml file based on the keys. The load() function has used in the script to read the full content of the customer.yaml file. This function will return the content of the file as a python listing of dictionaries. Side by side, the dump() role is used to convert the list into a YAML stream that has been printed later.

# Import YAML module

import yaml

# Load YAML information from the file

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

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

# Print YAML data earlier sorting

print (read_data)

# Sort YAML data based on keys

sorted_data = yaml.dump (read_data)

# Print YAML data after sorting

print (sorted_data)

Output:

The following output will announced later on executing the above script. After converting the content of the client.yaml file into a python list of dictionaries, each dictionary of the python listing has converted into each fellow member of the YAML content like the previous case. The value of the sort_key parameter of the dump() function is set to Truthful by default. And then, the output shows the sorted YAML content based on the keys.

Example-iii: Read the keys and values from a YAML file

Create a python file with the following script to read and impress the key and value separately from the client.yaml file. After loading the file'south content into the read_data variable, the item() function has used to read each key and the corresponding value from the content. The nested 'for' loop has used to iterate the full content of the file and impress the primal-value pairs.

# Import YAML module

import yaml

# Load the YAML file

with open ( 'customer.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 information

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

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

print (cardinal, ":" , value)

print ( '' )

Output:

The following output will appear after executing the above script. The file'due south content has not been sorted because the dump() role has not been used in the script.

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

The safe_load() function is used to convert the content of the YAML file into the python list of the dictionaries. This function can be used to load data 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 ( 'client.yaml' ) as fh:

# Convert the YAML data into a dictionary

    dictionary_data = yaml.safe_load (fh)

# Print the dictionary information

print (dictionary_data)

Output:

The following output will announced afterwards executing the higher up script. A list of dictionaries has been printed in the output.

Conclusion:

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

About the writer

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

bellintmortion.blogspot.com

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

Post a Comment for "Read Yaml Files in Python Line by Line"