小记】configparser读取ini配置文件"/>
【python路上小记】configparser读取ini配置文件
新建ini文件
clientconf.ini 写入一些数据
ini文件中 分号;是注释的意思
;Database 数据库信息[PostgreHost]
host = 127.0.0.1
port = 5432
user = pgsql
passwd = pgsql
db = pgsql123
model = model123
table = table123
Configparser模块读取
# coding=utf-8
import configparser
import os# 读取ini数据库配置
class PostgreHost():conf = configparser.RawConfigParser()conf.read("./clientconf.ini", encoding="utf-8")host = conf.get("PostgreHost", "host")port = conf.get("PostgreHost", "port")user= conf.get("PostgreHost", "user")passwd = conf.get("PostgreHost", "passwd")db = conf.get("PostgreHost", "db")model = conf.get("PostgreHost", "model")table = conf.get("PostgreHost", "table")# 可以直接配置成字典
DB_URI_PG = {'host': conf.get("PostgreHost", "host"),'port': conf.get("PostgreHost", "port"),'user': conf.get("PostgreHost", "user"),'passwd': conf.get("PostgreHost", "passwd"),'db': conf.get("PostgreHost", "db")}
更多推荐
【python路上小记】configparser读取ini配置文件
发布评论