Get the App
SLTechnology News&Howtos  ›  Development  › 

How Python3 reads toml configuration files

Shulou Source: shulou.com Published: 2022-06-03 04:30:23 11月17日 Update

This article mainly introduces Python3 how to read the toml configuration file, the article is very detailed, has a certain reference value, interested friends must read it!

[introduction to toml]

TOML is a language created in 2013 by the former GitHub CEO, Tom Preston-Werner, with the goal of becoming a small, easy-to-use semantic profile format. TOML is designed to be converted into a hash table (Hash table) without ambiguity.

The official Chinese introduction is here: https://github.com/toml-lang/toml/tree/master/versions/cn

The third-party parsing package used under walker is: https://pypi.org/project/toml/

[config.toml]

# input directory SrcRoot ='D:\ test\ input'# output directory DstRoot ='D:\ test\ output'

[t.py]

# encoding: utf-8#author: walker#date: 2018-12-11#summary: read toml configuration file import osimport sysimport tomlSrcRoot = r''DstRoot = ringing configuration # read configuration file def ReadConfig (): global SrcRoot, DstRoot cfgFile = 'config.toml' if not os.path.exists (cfgFile): input (cfgFile +' not found') sys.exit (- 1) with open (cfgFile) Mode='rb') as f: content = f.read () if content.startswith (b'\ xef\ xbb\ xbf'): # remove the utf8 bom header content = content [3:] dic = toml.loads (content.decode ('utf8')) SrcRoot = dic [' SrcRoot'] .strip (SrcRoot): print ('Error: not exists% s' % SrcRoot) sys.exit (- 1) print ('SrcRoot:% s'% SrcRoot) DstRoot = dic ['DstRoot'] .strip () if not os.path.exists (DstRoot): print (' Error: not exists% s'% DstRoot) sys.exit (- 1) print ('DstRoot:% s'% DstRoot) print ('Read config.toml succeeded') If _ _ name__ = ='_ _ main__': ReadConfig ()

[cmd]

D:\ Python3Project\ test > python3 t.pySrcRoot: d:\ test\ inputDstRoot: d:\ test\ outputRead config.toml successed! These are all the contents of the article "how Python3 reads toml configuration files". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: File configuration content format catalog article ambiguity value interest official partner small rule small scale more goal knowledge third party introduction industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Tech Info vpn Shulou Technology MariaDB