Common ways to write configuration files in Python

Love to learn 2021-01-20 19:47:55
common ways write configuration files


Hello everyone , Let's talk today Python Configuration file in How to write ?.

Why write configuration files

In the development process , We often use some fixed parameters or constants . For these more fixed and commonly used parts , It is often written to a fixed file , Avoid repetition in different module code to keep the core code clean .

This fixed file can be written directly into a .py file , for example or, The advantage of this is that it can directly pass through under the same project import To import parts of it ; But if we need to be in other non Python When sharing configuration files on the platform of , Write it as a single .py It's not a good choice . In this case, we should choose the general configuration file type to store these fixed parts . At present, the commonly used and popular configuration file format types are mainly ini、json、toml、yaml、xml etc. , These types of configuration files can be parsed through standard library or third-party library .


ini namely Initialize Initialization means , Early on Windows The storage format of the configuration file on the .ini The writing of the document is easy to understand , It's often simpler , Usually by the section (Section)、 key (key) And the value (value) form , It's like this :

host =
user = root
password = 123456
port = 3306
database = mysql

Python Built in configparser Standard library , We can directly use it on ini File parsing . If we save the above content in a name called db.ini In the file of , And then use read() Method to parse and read , Finally through items() Method to get all the key value pairs under the specified node .

>>> from configparser import ConfigParser
>>> cfg = ConfigParser()
>>> cfg.items("localdb")
[('host', ''), ('user', 'root'), ('password', '123456'), ('port', '3306'), ('database', 'mysql')]

It should be noted that ,configparser By default, the value is rendered as a string , So that's why we're in db.ini There are no quotation marks in the document, but the reason why the literal quantity is written directly on it .

After getting the key value pair , I actually converted it into a dictionary , And then through unpacking the way to wear reference , Keep the code simple :

#!pip install pymysql
import pymysql
from configparser import ConfigParser
cfg = ConfigParser()"/Users/Bobot/db.ini")
db_cfg = dict(cfg.items("localdb"))
con = pymysql.connect(**db_cfg)


json Format can be said to be a common form of documents , It is also a popular data exchange format on the Internet . besides ,json Sometimes it's a configuration file .

such as npm(JavaScript Package management tools are similar to Python Of pip)、 And Microsoft's widely used VSCode Editor , All use json Write configuration parameters .

and configparser equally ,Python Also built in json Standard library , Can pass load() and loads() Method to import files and strings json Content .

"host": "",
"user": "root",
"password": "123456",
"port": 3306,
"database": "mysql"

We save the above as db.json After reading and parsing ,json Library to read json The file is relatively simple and easy , And it's easy to parse into Python The dictionary object of .

>>> import json
>>> from pprint import pprint
>>> with open('/Users/Bobot/db.json') as j:
...  cfg = json.load(j)['localdb']
>>> pprint(cfg)
{'database': 'mysql',
'host': '',
'password': '123456',
'port': 3306,
'user': 'root'}

Use json The disadvantage of file configuration is that syntax standards are strictly limited , One of the criticisms is that you can't write notes in it , Unless you take json Other supersets of type as alternatives (VSCode You can write notes in json The parameter configuration file is one of the alternatives ); At the same time, there is the problem of too deep nesting , It's easy to make mistakes , It should not be used to write too long or complex parameter configuration information .


toml Format ( or tml Format ) yes Github cofounder Tom Preston-Werner A configuration file format is proposed . According to Wikipedia ,toml It was first put forward in 2013 year 7 month , Seven years ago ; It's also in some ways related to what we're going to talk about later yaml The document is somewhat similar to , But if you know yaml There are dozens of pages in the specification of ( No mistake. , It's really dozens of pages ……) When , Maybe you really don't want to write such a complicated configuration file ,toml The format is a good choice .

toml The format is roughly as follows : file

You can see it here toml It's kind of similar to what I said before ini file . But it's better than ini Expanded more content .

In the sample image, we can see , In addition to the basic string , For example, time stamp 、 Boolean value 、 Arrays and so on are further supported , And the style and Python The original way of writing is very similar .

Of course, there won't be too much introduction here toml Some specifications of the format , Someone has translated the official specification document , Interested friends can check directly .

It's a good fit Python The configuration file type has been created by developers 「 wheel 」, Currently in Github On Stars The largest number is uiri/toml Version of , But the version only passed v0.5 edition toml standard , But it is very simple in use , We can go through pip Command to install

pip install toml

The parsing of this library is very simple , It's a little bit like json The analytic usage of the library , That is, through load() or loads() To parse ; In the same way, the same is true of conversion and export .

For example, we now write the following into config.toml in :

host = ""
user = "root"
port = 3306
database = "test"
pool_size = 5
charset = "utf8"
pandas_cols = [ "id", "name", "age", "date"]

And then we can go through toml In the library load() Method to read :

>>> import toml
>>> import os
>>> from pprint import pprint
>>> cfg = toml.load(os.path.expanduser("~/Desktop/config.toml"))
>>> pprint(cfg)
{'mysql': {'database': 'test',
'fields': {'pandas_cols': ['id', 'name', 'age', 'date']},
'host': '',
'parameters': {'charset': 'utf8', 'pool_size': 5},
'port': 3306,
'user': 'root'}}

You can see toml Files are indirectly converted to dictionary types , Of course, this is also json How to write the version ( Replace single quotation marks with double quotation marks ), It is convenient for us to call or pass parameters later .


yaml Format ( or yml Format ) It is a popular configuration file at present , It was as early as 2001 By a person named Clark Evans The people who put forward ; At the same time, it is also a widely used configuration file type , Typical is Docker In the container docker-compose.yml The configuration file , If you use Docker It's no stranger to those who deploy .

yaml The design of the document is from Python、XML And other places to get inspiration , So you can see the shadow of these parts clearly when you use it .

In the last section toml I mentioned in the content that ,yaml The content of the specification can be said to be lengthy and complex , A good 80 There are so many pages ( Fight the strong , Terror is like this ……).


So interested friends can learn the relevant usage by themselves .

YAML Officials have already provided the corresponding Python Library to support , namely PyYAML; Of course, we also need to install it in advance :

pip install pyyaml

Same as json Kuhe toml Like the library , adopt load() Method to load .

It should be noted that , Use load() There are some security risks in this method , From Cisco Talos In this report, we can see that , If unknown or untrusted yaml file , Then there may be the risk of being attacked and network security risks , Because it can directly call the corresponding Python Function to execute the command needed by the attacker , For example, in yaml Write this paragraph in the file :

# Use Linux and macOS Don't try it easily 
!!python/object/apply:os.system ["rm -rf /"]

So it's better to use safe_load() Instead of load() Method .

This sum Python Built in string Standard library Template Class substitute() Template method has the same security risks , So use safe_substitute() It's the same thing to replace .

For example, we will now write some of the previous configuration information to config.yaml In file :

host: ""
port: 3306
user: "root"
password: "123456"
database: "test"
pool_size: 5
charset: "utf8"
- id
- name
- age
- date

And then we passed safe_load() Method :

>>> import os
>>> from pprint import pprint
>>> with open(os.path.expanduser("~/config.yaml"), "r") as config:
...  cfg = yaml.safe_load(config)
>>> pprint(cfg)
{'mysql': {'database': 'test',
'fields': {'pandas_cols': ['id', 'name', 'age', 'date']},
'host': '',
'parameter': {'charset': 'utf8', 'pool_size': 5},
'password': '123456',
'port': 3306,
'user': 'root'}}

You can see the final result and the preceding toml The analysis results of the library are basically consistent .


This article lists some mainstream and common configuration file types and their Python The reading method of , Some readers may find that there is no xml Format type content . about xml The configuration file may be associated with Java Friends who deal with language will meet more , but xml The readability of the document is really daunting ; Yes xml Friends who don't know the file can use Chrome The browser randomly enters a website and presses F12 After entering the developer, check out the dense html The element is .xml The epitome of .

In addition to these mainstream profile types , Like some .cfg、.properties Can be used as a configuration file , Even as mentioned at the beginning , You use one alone .py File to write all kinds of configuration information as a configuration file to import is no problem , It's just that there may be barriers to cross language sharing . Therefore, this article only introduces , Interested friends can learn more about .

The complexity of the configuration file types listed in this article increases from top to bottom :ini < json ≈ toml < yaml, There are advantages and disadvantages between them , You can choose according to your actual needs and team cooperation requirements .

author :100gle source :Python The Chinese community

[ Take it with you !Python 3.9 Official Chinese documents , Time limited collection !] (

[ Time limit ! Quick collar !14 Zhang HD Python Quick reference table , It is necessary to improve efficiency !] (

[GitHub Star sign 3W+,80 individual Python Case study , Take you easy to play Python Study !] (

本文为[Love to learn]所创,转载请带上原文链接,感谢

  1. 利用Python爬虫获取招聘网站职位信息
  2. Using Python crawler to obtain job information of recruitment website
  3. Several highly rated Python libraries arrow, jsonpath, psutil and tenacity are recommended
  4. Python装饰器
  5. Python实现LDAP认证
  6. Python decorator
  7. Implementing LDAP authentication with Python
  8. Vscode configures Python development environment!
  9. In Python, how dare you say you can't log module? ️
  10. 我收藏的有关Python的电子书和资料
  11. python 中 lambda的一些tips
  12. python中字典的一些tips
  13. python 用生成器生成斐波那契数列
  14. python脚本转pyc踩了个坑。。。
  15. My collection of e-books and materials about Python
  16. Some tips of lambda in Python
  17. Some tips of dictionary in Python
  18. Using Python generator to generate Fibonacci sequence
  19. The conversion of Python script to PyC stepped on a pit...
  20. Python游戏开发,pygame模块,Python实现扫雷小游戏
  21. Python game development, pyGame module, python implementation of minesweeping games
  22. Python实用工具,email模块,Python实现邮件远程控制自己电脑
  23. Python utility, email module, python realizes mail remote control of its own computer
  24. 毫无头绪的自学Python,你可能连门槛都摸不到!【最佳学习路线】
  25. Python读取二进制文件代码方法解析
  26. Python字典的实现原理
  27. Without a clue, you may not even touch the threshold【 Best learning route]
  28. Parsing method of Python reading binary file code
  29. Implementation principle of Python dictionary
  30. You must know the function of pandas to parse JSON data - JSON_ normalize()
  31. Python实用案例,私人定制,Python自动化生成爱豆专属2021日历
  32. Python practical case, private customization, python automatic generation of Adu exclusive 2021 calendar
  33. 《Python实例》震惊了,用Python这么简单实现了聊天系统的脏话,广告检测
  34. "Python instance" was shocked and realized the dirty words and advertisement detection of the chat system in Python
  35. Convolutional neural network processing sequence for Python deep learning
  36. Python data structure and algorithm (1) -- enum type enum
  37. 超全大厂算法岗百问百答(推荐系统/机器学习/深度学习/C++/Spark/python)
  38. 【Python进阶】你真的明白NumPy中的ndarray吗?
  39. All questions and answers for algorithm posts of super large factories (recommended system / machine learning / deep learning / C + + / spark / Python)
  40. [advanced Python] do you really understand ndarray in numpy?
  41. 【Python进阶】Python进阶专栏栏主自述:不忘初心,砥砺前行
  42. [advanced Python] Python advanced column main readme: never forget the original intention and forge ahead
  43. python垃圾回收和缓存管理
  44. java调用Python程序
  45. java调用Python程序
  46. Python常用函数有哪些?Python基础入门课程
  47. Python garbage collection and cache management
  48. Java calling Python program
  49. Java calling Python program
  50. What functions are commonly used in Python? Introduction to Python Basics
  51. Python basic knowledge
  52. Anaconda5.2 安装 Python 库(MySQLdb)的方法
  53. Python实现对脑电数据情绪分析
  54. Anaconda 5.2 method of installing Python Library (mysqldb)
  55. Python implements emotion analysis of EEG data
  56. Master some advanced usage of Python in 30 seconds, which makes others envy it
  57. python爬取百度图片并对图片做一系列处理
  58. Python crawls Baidu pictures and does a series of processing on them
  59. python链接mysql数据库
  60. Python link MySQL database