
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 settings.py or config.py, 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 Not Python The platform of When sharing configuration files , 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 :
[localdb]
host = 127.0.0.1
user = root
password = 123456
port = 3306
database = mysqlPython 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.read("/Users/Bobot/db.ini")
['/Users/Bobot/db.ini']
>>> cfg.items("localdb")
[('host', '127.0.0.1'), ('user', 'root'), ('password', '123456'), ('port', '3306'), ('database', 'mysql')] It should be noted that ,configparser The default value will be In the form of strings present , 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()
cfg.read("/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 .
{
"localdb":{
"host": "127.0.0.1",
"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': '127.0.0.1',
'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 You can't write notes in it , Unless you take json Other types of Superset As an alternative (VSCode You can write notes in json The parameter configuration file is one of the alternatives ); At the same time Nesting too deep The problem of , 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 :

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 :
[mysql]
host = "127.0.0.1"
user = "root"
port = 3306
database = "test"
[mysql.parameters]
pool_size = 5
charset = "utf8"
[mysql.fields]
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': '127.0.0.1',
'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() Method There will be certain security risks , 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 :
mysql:
host: "127.0.0.1"
port: 3306
user: "root"
password: "123456"
database: "test"
parameter:
pool_size: 5
charset: "utf8"
fields:
pandas_cols:
- 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': '127.0.0.1',
'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 .
source : The Internet
Recommended reading :
introduction : The most complete zero Foundation Python The problem of | Zero Basics 8 Months Python | Actual project | learn Python That's the shortcut
dried food : A short comment on crawling Douban , The movie 《 The rest of us 》 | 38 year NBA Best player analysis | From people's expectation to public praise ! Tang Dynasty detective 3 disappointing | Laugh at the story of the new Yitian dragon slaying | Riddle answer King | use Python Make a massive sketch of my little sister | Mission impossible is so hot , I use machine learning to make a mini recommendation system movie
Interest : Pinball game | squared paper for practicing calligraphy | Beautiful flowers | Two hundred lines Python《 Cool run every day 》 game !
AI: A robot that can write poetry | Color the picture | Forecast revenue | Mission impossible is so hot , I use machine learning to make a mini recommendation system movie
Gadget : Pdf turn Word, Easily handle forms and watermarks ! | One touch html Save the page as pdf!| bye PDF Withdrawal charges ! | use 90 Lines of code create the strongest PDF converter ,word、PPT、excel、markdown、html One click conversion | Make a nail low-cost ticket reminder ! |60 Line of code to do a voice wallpaper switcher, look at my little sister every day !|Annual hot money copy
1). Oh my god !Pdf turn Word use Python Easy to handle !
2). learn Python It's delicious ! I use 100 Line of code to make a website , Help people PS Travel pictures , Earn a chicken leg to eat
3). Premiere billions , Hot all over the net , I analyzed 《 My sister 》, Discovered the secrets
4).80 Line code ! use Python Make a dorai A Dream separation
5). What you have to master 20 individual python Code , short , Useful
6).30 individual Python Strange sexual skills Collection
7). I summed up 80 page 《 Rookie Science Python Select dry goods .pdf》, Is dry
8). bye Python! I have to learn Go 了 !2500 Word depth analysis !
9). Found a licking dog welfare ! This Python Reptile artifact is great , Automatically download sister pictures
Click to read the original , see B My station 20 A video !