TOML and YAML file formats are used to store configuration data for software applications. This article discusses how to convert TOML data to YAML format in Python.
What is The TOML File Format?
TOML is a configuration file format designed to be easy to read and write for both humans and machines. The TOML format uses a syntax similar to the INI file format. In a TOML file, we store the data as key-value pairs using the assignment operator. Keys are on the left side of the equal sign (=), and values are on the right side. Values can be of various types, such as strings, numbers, booleans, and arrays.
We can organize the key-value pairs into tables. Each table is defined using square brackets []
. We can store as many key-value pairs in a table. To understand this, consider the following example.
[employee]
name = "John Doe"
age = 35
[job]
title = "Software Engineer"
department = "IT"
years_of_experience = 10
[address]
street = "123 Main St."
city = "San Francisco"
state = "CA"
zip = 94102
The above example contains the data of an employee in the TOML format. The data is structured into three tables i.e. [employee]
, [job]
, and [address]
. Here, each table contains different key-value pairs.
- The
[employee]
section contains thename
andage
of the employee. [job]
section contains information about the employee’s job, including their job title, department, and years of experience.- The
[address]
section contains the employee’s address information, including their street address, city, state, and zip code.
What is The YAML File Format?
The YAML format is a data serialization format commonly used for configuration files and data interchange. It also uses key-value pairs to represent data. Here, Key-value pairs are written as <key>: <value>
. The colon (:) separates the key and value, and space follows the colon.
Unlike the TOML format, YAML uses indentation and whitespace to represent the structure of data. The indentation levels in the configuration file define the hierarchy and nesting of data elements. For example, we can represent the data shown in the TOML example in YAML format as shown below.
address:
city: San Francisco
state: CA
street: 123 Main St.
zip: 94102
employee:
age: 35
name: John Doe
job:
department: IT
title: Software Engineer
years_of_experience: 10
Both the TOML and YAML file formats are popular formats for storing configuration data. However, we might sometimes need to convert the files from one format to another. In the next sections, we will discuss how to convert the TOML file format to YAML in Python.
Convert TOML String to YAML String in Python
We will use the toml and the yaml module to convert a TOML string to a YAML string in Python. First, we will convert the TOML string to a Python dictionary. For this, we will use the loads()
method defined in the toml module. The loads()
method takes the TOML string as its input argument and returns a Python dictionary.
Next, we will convert the Python dictionary to a yaml string. For this, we will use the dump()
method defined in the yaml module. The dump()
method takes the Python dictionary as its input argument and returns the output YAML string. You can observe this in the following example.
import toml
import yaml
toml_string="""[employee]
name = "John Doe"
age = 35
[job]
title = "Software Engineer"
department = "IT"
years_of_experience = 10
[address]
street = "123 Main St."
city = "San Francisco"
state = "CA"
zip = 94102
"""
python_dict=toml.loads(toml_string)
yaml_string=yaml.dump(python_dict)
print("The TOML string is:")
print(toml_string)
print("The YAML string is:")
print(yaml_string)
Output:
The TOML string is:
[employee]
name = "John Doe"
age = 35
[job]
title = "Software Engineer"
department = "IT"
years_of_experience = 10
[address]
street = "123 Main St."
city = "San Francisco"
state = "CA"
zip = 94102
The YAML string is:
address:
city: San Francisco
state: CA
street: 123 Main St.
zip: 94102
employee:
age: 35
name: John Doe
job:
department: IT
title: Software Engineer
years_of_experience: 10
Convert TOML String to a YAML File in Python
Instead of creating a string, we can also convert a TOML string to a YAML file in Python. For this, we will use the following steps.
- First, we will open an empty YAML file in write mode using the
open()
function. Theopen()
function takes the file name as its first argument and the Python literal“w”
as its second input argument. After execution, it returns a file pointer. - Next, we will convert the TOML string to a Python dictionary using the
loads()
method defined in the toml module. - After this, we will use the
dump()
method defined in the yaml module to save the dictionary to the file in YAML format. For this, we will pass the dictionary as the first input argument and the file pointer to the YAML file as the second argument. After execution, thedump()
method will write the data in YAML format into the file. - Finally, we will close the file using the
close()
method.
After executing the above steps, the TOML string will be saved in the YAML format into the file. You can observe this in the following example.
import toml
import yaml
toml_string="""[employee]
name = "John Doe"
age = 35
[job]
title = "Software Engineer"
department = "IT"
years_of_experience = 10
[address]
street = "123 Main St."
city = "San Francisco"
state = "CA"
zip = 94102
"""
yaml_file=open("employee.yaml","w")
python_dict=toml.loads(toml_string)
yaml.dump(python_dict,yaml_file)
yaml_file.close()
The output file looks as follows.
TOML File to YAML String in Python
Instead of strings, we can also convert TOML files to the YAML format in Python. For this, we will use the following TOML file.
To convert the TOML file to a YAML string in Python, we will use the following steps.
- First, we will open the TOML file in read mode using the
open()
function. - Next, we will read the TOML file into a Python dictionary. For this, we will use the
load()
method defined in the toml module. Theload()
method takes the file pointer to the TOML file as its input argument and returns a Python dictionary containing the data from the file. - Once we get the dictionary, we will convert it to a YAML string using the
dump()
method defined in the yaml module. - Finally, we will close the TOML file using the
close()
method.
After executing the above steps, we can easily convert a TOML file to a YAML string. You can observe this in the following example.
import toml
import yaml
toml_file=open("employee.toml","r")
python_dict=toml.load(toml_file)
yaml_string=yaml.dump(python_dict)
toml_file.close()
print("The YAML string is:")
print(yaml_string)
Output:
The YAML string is:
address:
city: San Francisco
state: CA
street: 123 Main St.
zip: 94102
employee:
age: 35
name: John Doe
job:
department: IT
title: Software Engineer
years_of_experience: 10
Convert TOML File to YAML File in Python
To convert a TOML file to a YAML file in Python, we will first open the TOML file in read mode and an empty YAML file in write mode using the open()
function. Then, we will read the TOML file into a Python dictionary using the load()
method defined in the toml module. After this, we will write the data into the YAML file using the dump()
method defined in the yaml module. Finally, we will close both files using the close()
method as shown below.
import toml
import yaml
toml_file=open("employee.toml","r")
yaml_file=open("employee.yaml","w")
python_dict=toml.load(toml_file)
yaml.dump(python_dict,yaml_file)
yaml_file.close()
toml_file.close()
Conclusion
In this article, we discussed how to convert a TOML file or string to YAML format in Python. To learn more about Python programming, you can read this article on list vs set in Python. You might also like this article on if vs elif vs else in python.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Happy Learning!
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.