##language:zh ''' 文章来自《Python cookbook》. 翻译仅仅是为了个人学习,其它商业版权纠纷与此无关! ''' -- 61.182.251.99 [<>] <> = 描述 = Reading INI Configuration Files 读取INI配置文件 Credit: Dirk Holtwick == 问题 Problem == You want to load a configuration file for your program, but you don't want to use a Python module for this purpose, as that might expose you to security risks or troublesome syntax and other errors in the module. 程序需要读取配置文件。 不想用一个可能带有安全漏洞、有复杂格式或者这样那样错误的(自己开发的)模块实现此配置功能。 == 解决 Solution == The standard ConfigParser library module gives us almost all we need to use INI files for configuration: 标准库模块'''ConfigParser'''几乎提供了解析INI配置文件所需的一切: {{{ #!python import ConfigParser import string _ConfigDefault = { "database.dbms": "mysql", "database.name": "", "database.user": "root", "database.password": "", "database.host": "127.0.0.1" } def LoadConfig(file, config={}): """ returns a dictionary with keys of the form
.