使用Python中的config配置
来源:互联网 发布:第三方软件下载 编辑:程序博客网 时间:2024/05/29 15:15
[Python]使用Python中的config配置
Python中有ConfigParser类,可以很方便的从配置文件中读取数据(如DB的配置,路径的配置),所以可以自己写一个函数,实现读取config配置。
config文件的写法比较简单,[section]下配置key=value,一下是例子:db.conf
#配置数据库[database]dbhost=127.0.0.1dbport=3366dbname=testdbuser=testdbpassword=testdbcharset=utf8
接着写一个读取config的方法模块
#encoding:utf-8#name:mod_config.pyimport ConfigParserimport os#获取config配置文件def getConfig(section, key): config = ConfigParser.ConfigParser() path = os.path.split(os.path.realpath(__file__))[0] + '/db.conf' config.read(path) return config.get(section, key)#其中 os.path.split(os.path.realpath(__file__))[0] 得到的是当前文件模块的目录
当需要在文件中读取config的配置时,就载入这个模块,调用getConfig方法。
import mod_configdbname = mod_config.getConfig("database", "dbname")
阅读全文
0 0
- 使用Python中的config配置
- Struts2零配置中的config-browser插件的使用问题
- Config配置的使用
- web.config中的session配置
- 配置web.config中的 membership
- web.config中的httpRuntime配置
- app/config 中的配置说明
- 使用Python的logging.config
- 使用Python的logging.config
- Linux中的pkg-config使用
- 在Web.config或App.config中的添加自定义配置
- 在Web.config或App.config中的添加自定义配置
- 在Web.config或App.config中的添加自定义配置
- ASP.NET中使用web.config配置web应用程序中的数据库连接
- ASP.NET中使用web.config配置web应用程序中的数据库连接
- ASP.NET中使用web.config配置web应用程序中的数据库连接
- ASP.NET中使用web.config配置web应用程序中的数据库连接
- ASP.NET中使用web.config配置web应用程序中的数据库
- 明年的昨天算法 / ios时间格式bug修复
- linux一些实用方法
- 集装箱编号校验码规则及java/JavaScript的实现
- 优达学城《计算机科学导论》 --判断单位矩阵
- LVS-DR
- 使用Python中的config配置
- 数据库存储为null时,自定义字符标识。使转为json数据后,列不丢失或有undefined。
- JAVA入门--------笔记
- C语言之常量(笔记三)
- 设计模式之禅笔记-策略模式
- Core Java, Volume II--Advanced Features (10th Edition) (Core Series) 电子书(手机)
- 学习笔记-Map映射
- 函数数组
- SAP-采购到应付过程中的会计分录