您的位置:首页 > 其它

11.2 configparser--配置文件分析库

2016-05-22 21:52 190 查看
本模块主要提供了一个类ConfigParser,它实现对结构化的文件,类似Windows的INI的文件进行操作。通过INI文件就可以对Python程序进行定制化的配置。 生成一个INI配置文件,可以通过下面的例子来实现这个目标:#python 3.4import configparserconfig = configparser.ConfigParser()config['DEFAULT'] = {'ServerAliveInterval': '45', 'Compression': 'yes', 'CompressionLevel': '9'}config['bitbucket.org'] = {}config['bitbucket.org']['User'] = 'hg'config['topsecret.server.com'] = {}topsecret = config['topsecret.server.com']topsecret['Port'] = '50022' # mutates the parsertopsecret['ForwardX11'] = 'no' # same hereconfig['DEFAULT']['ForwardX11'] = 'yes'with open('example.ini', 'w') as configfile: config.write(configfile)在这个例子里,先创建一个ConfigParser对象,然后采用字典访问的方式进行保存数据,语句config['DEFAULT']就是创建一段配置,生成的INI文件如下:[DEFAULT]compression = yesserveraliveinterval = 45compressionlevel = 9forwardx11 = yes像语句config['bitbucket.org'] = {}这样就是生成一段INI,像语句config['bitbucket.org']['User'] = 'hg'就是对其中一个参数进行赋值。最后,调用open()函数打开一个文件进行写入这些参数,生成的INI文件内容如下:[DEFAULT]compression = yesserveraliveinterval = 45compressionlevel = 9forwardx11 = yes [bitbucket.org]user = hg [topsecret.server.com]port = 50022forwardx11 = no 对一个已经存在的INI文件进行读取的例子:#python 3.4import configparserconfig = configparser.ConfigParser()r = config.read('example.ini')print(r)print(config.defaults())print(config.sections())结果输出如下:['example.ini']OrderedDict([('compression', 'yes'), ('serveraliveinterval', '45'), ('compressionlevel', '9'), ('forwardx11', 'yes')])['bitbucket.org', 'topsecret.server.com'] class configparser.ConfigParser(defaults=None, dict_type=collections.OrderedDict, allow_no_value=False, delimiters=('=', ':'), comment_prefixes=('#', ';'), inline_comment_prefixes=None, strict=True, empty_lines_in_values=True, default_section=configparser.DEFAULTSECT, interpolation=BasicInterpolation()) 创建一个配置分析器。参数defaults给出时,表示字典里使用这些初始化值;参数dict_type是用来创建所有段的字典列表;参数allow_no_value设置为True时,可选的参数可以为空;参数delimiters是键和值是分成不同的集合。 defaults() 返回缺省段所有参数,以排序字典的方式返回。 sections() 返回所有可用的配置段,默认段不包括在内。 add_section(section) 添加一段到INI文件,参数section是段名称。如果添加已经存在的段会抛出异常DuplicateSectionError错误。如果添加default段,抛出异常ValueError。 has_section(section) 查询段section是否已经存在。查询default段不作响应。 options(section) 返回这段下面所有的选项。 has_option(section, option) 查询指定的段中的选项是否存在,如果存在返回True,否则返回False。 read(filenames, encoding=None) 读取配置文件filenames,然后分析文件所有选项。 read_file(f, source=None) 从对象f里读取数据,并分析配置数据。 read_string(string, source='<string>') 从字符串里分析配置数据。 read_dict(dictionary, source='<dict>') 从字典对象里读取配置参数。 get(section, option, *, raw=False, vars=None[, fallback]) 从指定段里获取一个选项的参数。 getint(section, option, *, raw=False, vars=None[, fallback]) 从指定段里获取一个选项的参数,以整数方式返回。 getfloat(section, option, *, raw=False, vars=None[, fallback]) 从指定段里获取一个选项的参数,以浮点数方式返回。 getboolean(section, option, *, raw=False, vars=None[, fallback]) 从指定段里获取一个选项的参数,以布尔值方式返回。 items(raw=False, vars=None) items(section, raw=False, vars=None) 当没有给段名称时,直接返回所有段的列表;当给出段名称时,返回该段所有选项列表。 set(section, option, value) 给指定段的选项设置值,如果不存在抛出异常。 write(fileobject, space_around_delimiters=True) 写配置信息到指定的文件对象fileobject。参数space_around_delimiters为True时在分隔符两边添加空格,否则不添加。 remove_option(section, option) 从指定段里删除选项。 remove_section(section) 从配置文件里删除指定段。 optionxform(option) 把配置文件里的指定选项转换为内部的结构。 readfp(fp, filename=None) 准备不再使用的函数。 configparser.MAX_INTERPOLATION_DEPTH 最大的递归深度。 蔡军生 QQ:9073204 深圳
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: