{"id":1651,"date":"2024-03-01T18:43:55","date_gmt":"2024-03-01T13:13:55","guid":{"rendered":"https:\/\/geekpython.in\/?p=1651"},"modified":"2024-03-01T18:43:56","modified_gmt":"2024-03-01T13:13:56","slug":"tomllib-parse-toml-files-using-python","status":"publish","type":"post","link":"https:\/\/geekpython.in\/tomllib-parse-toml-files-using-python","title":{"rendered":"tomllib &#8211; Parse TOML files Using Python"},"content":{"rendered":"\n<p>You might have seen files with the <code>.toml<\/code> extension in programming projects. <strong>What is that TOML?<\/strong><\/p>\n\n\n\n<p><a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/toml.io\/en\/v0.5.0\"><strong>TOML<\/strong><\/a> (Tom&#8217;s Obvious Minimal Language) files can be used to store application metadata in an easily readable format. Its format is extremely simple due to its minimal syntax.<\/p>\n\n\n\n<p>The TOML file, like the JSON file in web development, is used to store application-specific configuration data, however, the syntax varies and is often preferred in projects where human readability and simplicity are prioritized.<\/p>\n\n\n\n<p>In this article, you will learn how to parse TOML files with the Python standard library <code><strong>tomllib<\/strong><\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">TOML File<\/h2>\n\n\n\n<p>TOML file contains data in key-value pairs and has native types such as string, integer, float, booleans, tables, arrays, and more.<\/p>\n\n\n\n<p>Here&#8217;s an example of a TOML file.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ini decode:true \" title=\"A TOML file\" ># This is a TOML configuration file\n\ntitle = \"TOML Config File\"\n\n[author]\nname = \"John Doe\"\ndob = 1979-05-27T07:32:00-08:00\n\n[app]\napp_name = \"Tomparse\"\nversion = 0.10\nsite.\"google.com\" = true\n\n[app.dependency]\nlibs = [\"tomllib\", \"tomli\"]\n\n[database]\nenabled = true\nports = [ 8000, 8001, 8002 ]\ndata = [ [\"delta\", \"phi\"], [3.14] ]\ntemp_targets = { cpu = 79.5, case = 72.0 }<\/pre><\/div>\n\n\n\n<p>In the above file, <code>title<\/code>, <code>owner<\/code>, <code>app<\/code>, <code>app.dependency<\/code>, and <code>database<\/code> are the keys, and <code>name<\/code>, <code>dob<\/code>, <code>app_name<\/code>, <code>version<\/code>, etc., are the subkeys.<\/p>\n\n\n\n<p>You can see values <code>[\"tomllib\", \"tomli\"]<\/code>, <code>[ [\"delta\", \"phi\"], [3.14] ]<\/code> are array and array of tables respectively and value <code>{ cpu = 79.5, case = 72.0 }<\/code> is an inline table.<\/p>\n\n\n\n<p>If the above file is put into JSON land, it would give the following structure.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" title=\"TOML converted to JSON\" >{\n  \"title\": \"TOML Config File\",\n  \"author\": {\n    \"name\": \"John Doe\",\n    \"dob\": \"1979-05-27T15:32:00.000Z\"\n  },\n  \"app\": {\n    \"app_name\": \"Tomparse\",\n    \"version\": 0.1,\n    \"site\": {\n      \"google.com\": true\n    },\n    \"dependency\": {\n      \"libs\": [\n        \"tomllib\",\n        \"tomli\"\n      ]\n    }\n  },\n  \"database\": {\n    \"enabled\": true,\n    \"ports\": [\n      8000,\n      8001,\n      8002\n    ],\n    \"data\": [\n      [\n        \"delta\",\n        \"phi\"\n      ],\n      [\n        3.14\n      ]\n    ],\n    \"temp_targets\": {\n      \"cpu\": 79.5,\n      \"case\": 72\n    }\n  }\n}<\/pre><\/div>\n\n\n\n<p>Now you can understand how TOML syntax works if you haven&#8217;t worked with the TOML files before.<\/p>\n\n\n\n<p>You can use TOML for storing Python project metadata. See <a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/peps.python.org\/pep-0621\/\">PEP 621<\/a> for more details.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">tomllib &#8211; Parsing TOML Files<\/h2>\n\n\n\n<p>With the release of Python 3.11, the <code>tomllib<\/code> is added to the Python standard library to parse the TOML files using Python. This library is intended to read TOML files.<\/p>\n\n\n\n<p>Having said that, the <code>tomllib<\/code> has only two functions: reading TOML from a file and loading TOML from a string.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Functions<\/h3>\n\n\n\n<p><code>tomllib.load(fp , parse_float=float)<\/code><\/p>\n\n\n\n<p>The <code>load()<\/code> function reads a TOML file.<\/p>\n\n\n\n<p><strong>Parameters:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>fp<\/code> &#8211; A readable and binary file object. You can pass it positionally, but not as a keyword argument.<\/li>\n\n\n\n<li><code>parse_float<\/code> &#8211; You can specify a custom function for parsing the float in the TOML file. It defaults to using Python&#8217;s <code>float()<\/code> function. This is a keyword-only argument.<\/li>\n<\/ul>\n\n\n\n<p><strong>Return value:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>load()<\/code> function returns the TOML file content in dictionary format.<\/li>\n<\/ul>\n\n\n\n<p><code>tomllib.loads(s, parse_float=float)<\/code><\/p>\n\n\n\n<p>The <code>loads()<\/code> function loads the TOML from a string object.<\/p>\n\n\n\n<p><strong>Parameters:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>s<\/code> or <code>string<\/code> &#8211; A string object that contains the TOML document.<\/li>\n\n\n\n<li><code>parse_float<\/code> &#8211; It is the same as the <code>load()<\/code> function&#8217;s <code>parse_float<\/code>.<\/li>\n<\/ul>\n\n\n\n<p><strong>Return value:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It also returns the results in dictionary format.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Parsing a TOML File<\/h2>\n\n\n\n<p>Here&#8217;s a <code>config.toml<\/code> file that contains some configuration data of an application written in TOML. You&#8217;ll use the <code>tomllib<\/code>&#8216;s <code>load()<\/code> function to parse the file.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python mark:6 decode:true \" >import tomllib\n\n# Opening a TOML file and reading in binary mode\nwith open(\"config.toml\", \"rb\") as tfile:\n    # Parsing TOML file content\n    result = tomllib.load(tfile)\n    print(result)<\/pre><\/div>\n\n\n\n<p>When you run the code, you&#8217;ll get the file&#8217;s content in a dictionary format.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tex decode:true \" >{'title': 'TOML Config File', 'owner': {'name': 'John Doe', 'dob': datetime.datetime(1979, 5, 27, 7, 32, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=57600)))}, 'app': {'app_name': 'Tomparse', 'version': 0.1, 'site': {'google.com': True}, 'dependency': {'libs': ['tomllib', 'tomli']}}, 'database': {'enabled': True, 'ports': [8000, 8001, 8002], 'data': [['delta', 'phi'], [3.14]], 'temp_targets': {'cpu': 79.5, 'case': 72.0}}}<\/pre><\/div>\n\n\n\n<p>Since the data is in dictionary format you can separate the keys and values from the data.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python mark:7-9 decode:true \" >import tomllib\n\n# Opening a TOML file and reading in binary mode\nwith open(\"config.toml\", \"rb\") as tfile:\n    # Parsing TOML file content\n    result = tomllib.load(tfile)\n    for key, value in result.items():\n        print(f\"Key: {key}\")\n        print(f\"Value: {value}\")<\/pre><\/div>\n\n\n\n<p>Now when you run this code, you&#8217;ll get the following result.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tex decode:true \" >Key: title\nValue: TOML Config File\nKey: author\nValue: {'name': 'John Doe', 'dob': datetime.datetime(1979, 5, 27, 7, 32, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=57600)))}\nKey: app\nValue: {'app_name': 'Tomparse', 'version': 0.1, 'site': {'google.com': True}, 'dependency': {'libs': ['tomllib', 'tomli']}}\nKey: database\nValue: {'enabled': True, 'ports': [8000, 8001, 8002], 'data': [['delta', 'phi'], [3.14]], 'temp_targets': {'cpu': 79.5, 'case': 72.0}}<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Exception Handling<\/h2>\n\n\n\n<p>The <code>tomllib<\/code> includes a <code>TOMLDecodeError<\/code> class that can handle errors encountered while decoding the TOML document.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python mark:12 decode:true \" >import tomllib\n\n# Opening a TOML file and reading in binary mode\ntry:\n    with open(\"config.toml\", \"rb\") as tfile:\n        # Parsing TOML file content\n        result = tomllib.load(tfile)\n        for key, value in result.items():\n            print(f\"Key: {key}\")\n            print(f\"Value: {value}\")\n\nexcept tomllib.TOMLDecodeError as e:\n    print(e)<\/pre><\/div>\n\n\n\n<p>As you can see, the code is wrapped in a <code>try-except<\/code> block, and any errors are handled by the <code>tomllib.TOMLDecodeError<\/code> in the <code>except<\/code> block.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tex decode:true \" >Expected '=' after a key in a key\/value pair (at line 12, column 18)<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Loading TOML from String<\/h2>\n\n\n\n<p>Assume you have a string with a TOML document. How do you parse that TOML? To achieve your desired result, use the <code>tomllib.loads()<\/code> function.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python mark:13 decode:true \" >import tomllib\n\nmy_toml = \"\"\"\n[app]\napp_name = \"Tomparse\"\nversion = 0.1\nsite.\"google.com\" = true\n\n[app.dependency]\nlibs = [\"tomllib\", \"tomli\"]\n\"\"\"\n\nload_toml = tomllib.loads(my_toml)\nprint(load_toml)<\/pre><\/div>\n\n\n\n<p>The variable <code>my_toml<\/code> contains a string of TOML, which is passed to the <code>tomllib.loads()<\/code> function.<\/p>\n\n\n\n<p>As usual, this code will return a dictionary and you&#8217;ll get the following result.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tex decode:true \" >{'app': {'app_name': 'Tomparse', 'version': 0.1, 'site': {'google.com': True}, 'dependency': {'libs': ['tomllib', 'tomli']}}}<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>So, if you write a configuration file or document in TOML and then want to parse it, you&#8217;ll know what to do.<\/p>\n\n\n\n<p>You can use the <code>tomllib<\/code> library, which was included in the Python standard library with the release of Python 3.11. This library includes functions that can be used to read TOML files.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\ud83c\udfc6<strong>Other articles you might be interested in if you liked this one<\/strong><\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/geekpython.in\/python-split-method\">Split your string into an array of words using the split() method in Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/geekpython.in\/map-function-in-python\">Map a function to each item in an iterable using the map() function<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/geekpython.in\/pickle-module-in-python\">Serialize and deserialize Python objects using the pickle module<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/geekpython.in\/understanding-if-__name__-__main__-in-python-programs\">Why if __name__ == \u2018__main__\u2019 is used in Python programs<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/geekpython.in\/build-websocket-server-and-client-using-python\">Create a WebSocket server and client in Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/geekpython.in\/render-images-from-flask\">Upload and display images on the frontend using Flask<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>That&#8217;s all for now<\/strong><\/p>\n\n\n\n<p><strong>Keep Coding\u270c\u270c<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You might have seen files with the .toml extension in programming projects. What is that TOML? TOML (Tom&#8217;s Obvious Minimal Language) files can be used to store application metadata in an easily readable format. Its format is extremely simple due to its minimal syntax. The TOML file, like the JSON file in web development, is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1654,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"","ocean_second_sidebar":"","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"","ocean_custom_header_template":"","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"","ocean_menu_typo_font_family":"","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"on","ocean_gallery_id":[],"footnotes":""},"categories":[2],"tags":[31,76],"class_list":["post-1651","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python3","tag-toml","entry","has-media"],"_links":{"self":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1651","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/comments?post=1651"}],"version-history":[{"count":3,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1651\/revisions"}],"predecessor-version":[{"id":1655,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1651\/revisions\/1655"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media\/1654"}],"wp:attachment":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media?parent=1651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/categories?post=1651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/tags?post=1651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}