其他分享
首页 > 其他分享> > ConfigParser是用于读取ini文件的配置内容

ConfigParser是用于读取ini文件的配置内容

作者:互联网

MyConf.py
#!/usr/bin/env python3
# -*- coding:utf-8 -*-

from configparser import ConfigParser

class MyConf(ConfigParser):

def __init__(self,filename):
super().__init__()
self.read(filename,encoding='utf-8')

比如从mysql.ini 读取配置信息
# 实例化配置类对象
conf = MyConf(os.path.join(Conf_dir, 'mysql.ini'))
# 1.数据库连接、生成游标
self.db = pymysql.connect(
host=conf.get("mysql", "host"),
user=conf.get("mysql", "user"),
password=conf.get("mysql", "passwd"),
port=conf.getint("mysql", "port"),
database=conf.get("mysql", "database"),
charset="utf8",
cursorclass=pymysql.cursors.DictCursor
)

标签:__,读取,get,self,ini,conf,mysql,ConfigParser
来源: https://www.cnblogs.com/newhope-2020/p/15854946.html