Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Python3 reads toml configuration files

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report