Four types of python configuration files: ini, json, toml, yaml
1. Configuration file: how to use ini
Create an ini file, file name: conf.ini
The conf.ini configuration file is as follows:
[mysql]
host='localhost'
user='root'
password='123456'
port=3306
db='future'
charset='utf8'
When reading the operation ini, you need to install the ConfigParser module
pip install configparser
code show as below:
#引用ConfigParser类
from configparser import ConfigParser
#实例化ConfigParser类
conf=ConfigParser()
#读取配置文件ini
conf.read("conf.ini",encoding="utf-8")
#读取配置文件某个字段
value=conf.get("mysql","host")
print(value)
---------------------打印结果---------------------
注:字符串格式
'localhost'
conf reading basic way
conf.get #读取出来默认字符串
conf.getboolean #读取出来bool
conf.getint #读取出来int
conf.getfloat #读取出来float
2. Configuration file: how to use yaml
YAML is a concise non-markup language
YAML is data-centric, using whitespace, indentation, and line breaks to organize data, resulting in a more concise presentation
basic rules:
1, case sensitive
2. Use indentation to represent hierarchical relationships
3, prohibit the use of tab indentation, only use the space bar
4. There is no limit to the indentation length, as long as the elements are aligned to indicate that these elements belong to the same level
5. Use "#" comments
6. Strings can be marked without quotation marks
Create a yaml file, file name: conf.yaml
-
yaml:
name: test
- host='localhost'
user='root'
password='123456'
- port=3306
db='future'
charset='utf8'
When reading and operating yaml, you need to install the pyyaml module
#安装方法
pip install pyyaml
code show as below:
#读取yaml
#引入yaml
import yaml
#打开yaml文件: open函数
fs = open("conf.yaml",encoding="utf-8")
#调用yaml.load加载文件对象,为python对象
s = yaml.load(fs,yaml.FullLoader)
print(s)
---------------------------打印结果---------------------------
[{'yaml': {'name': 'test'}}, "host='localhost' user='root' password='123456'", "port=3306 db='future' charset='utf8'"]
3. Configuration file: how to use json
Create a json file, file name: conf.json
{
"mysql": {
"host": "127.0.0.1",
"port": 3306,
"user": "root",
"password": "123456",
"database": "test"
},
"pic_path":{
"city": "上海"
}
}
When reading and operating json, you need to install the json module
pip install json
code show as below:
#引入json
import json
#打开conf文件: open函数
fs = open("conf.json",encoding="utf-8")
s=json.load(fs)
print(s["mysql"])
-------------------打印结果-------------------
{'database': 'test', 'password': '123456', 'user': 'root', 'host': '127.0.0.1', 'port': 3306}
Other operations of json
load() : 将json文件中读取json格式数据
loads(): 将字符串类型数据转化为json格式数据
dump(): 将json格式数据保存到文件
dumps(): 将json格式数据保存为字符串类型
4. Configuration file: how to use toml
The syntax of toml is widely composed of key = "value", [section name], #comment, supported data types: string, integer, floating point, boolean, date time, array
Create a toml file, file name: conf.toml
[mysql]
[mysql.config]
user = "root"
password = "123456"
When reading and operating toml, you need to install the toml module
pip install toml
code show as below:
import toml
fs = open("conf.toml",encoding="utf-8")
s= toml.load(fs)
print(s)
------------------------打印结果------------------------
{'mysql': {'config': {'password': '123456', 'user': 'root'}}}