By cn.Dixon
配置文件: config.ini
使用 %( )s 将字符串映射到变量
[mysql]
host = 172.0.0.1
port = 3306
db = default
user = root
password = root
jdbc_url = jdbc:mysql://%(host)s:%(port)s/%(db)s?user=%(user)s&password=%(password)s&characterEncoding=utf-8
读取配置文件:
from configparser import ConfigParser
cfp = ConfigParser()
cfp.read('config.ini', encoding='utf-8')
print(cfp.get('mysql', 'host')) # 172.0.0.1
print(cfp.getint('mysql', 'port')) # 3306
print(cfp.get('mysql', 'db')) # default
print(cfp.get('mysql', 'user')) # root
print(cfp.get('mysql', 'password')) # root
print(cfp.get('mysql', 'jdbc_url')) # jdbc:mysql://172.0.0.1:3306/default?user=root&password=root&characterEncoding=utf-8