【python路上小记】configparser读取ini配置文件

编程入门 行业动态 更新时间:2024-10-26 04:24:30

【python路上<a href=https://www.elefans.com/category/jswz/34/1764832.html style=小记】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配置文件

本文发布于:2023-07-28 18:48:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1278916.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小记   配置文件   路上   python   configparser

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!